Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ngozistephen's avatar

General error: 1364 Field 'owner_id' doesn't have a default value

Pls I been getting 500 error message; showing this General error: 1364 Field 'owner_id' doesn't have a default value (Connection: mysql, SQL: insert into properties (name, city_id, address_street, address_postcode, lat, long, updated_at, created_at) values (Central Hotel, 4, 16-18, Argyle Street Camden, WC1H 8EG, 51.529145, -0.1239401, 2023-05-21 23:08:59, 2023-05-21 23:08:59))

Migration file

  */
    public function up(): void
    {
        Schema::create('properties', function (Blueprint $table) {
            $table->id();
            $table->foreignId('owner_id')->constrained('users');
            $table->string('name');
            $table->foreignId('city_id')->constrained();
            $table->string('address_street');
            $table->string('address_postcode')->nullable();
            $table->decimal('lat', 10, 7)->nullable();
            $table->decimal('long', 10, 7)->nullable();
            $table->timestamps();
        });
    }

model

class Property extends Model
{
    use HasFactory;

    protected $fillable = [
        'owner_id',
        'name',
        'city_id',
        'address_street',
        'address_postcode',
        'lat',
        'long',
    ];

User migration file

public function up(): void
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

user model

     */
    protected $fillable = [
        'name',
        'email',
        'password',
        
    ];
0 likes
1 reply
LaryAI's avatar
Level 58

The error message indicates that the "owner_id" field in the "properties" table does not have a default value. This means that when you try to insert a new record into the "properties" table, you need to provide a value for the "owner_id" field.

One solution is to update your migration file to set a default value for the "owner_id" field. For example, you could set the default value to null:

$table->foreignId('owner_id')->nullable()->constrained('users');

Alternatively, you could update your code to always provide a value for the "owner_id" field when inserting a new record into the "properties" table.

$property = new Property;
$property->owner_id = $user->id;
$property->name = 'Central Hotel';
$property->city_id = 4;
$property->address_street = '16-18, Argyle Street Camden';
$property->address_postcode = 'WC1H 8EG';
$property->lat = 51.529145;
$property->long = -0.1239401;
$property->save();

Please or to participate in this conversation.