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

Stillfinder's avatar

SQLSTATE[HY000]: Field 'user_id' doesn't have a default value

Hello.

I'm trying to create a post from the Nova interface, but getting an error:

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `posts` (`title`, `excerpt`, `body`, `slug`, `updated_at`, `created_at`) values (asdf, asdf, <div>asdf</div>, asdf-1, 2020-04-04 06:26:10, 2020-04-04 06:26:10)) {"userId":1,"exception":"[object] (Illuminate\Database\QueryException(code: HY000): SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `posts` (`title`, `excerpt`, `body`, `slug`, `updated_at`, `created_at`) values (asdf, asdf, <div>asdf</div>, asdf-1, 2020-04-04 06:26:10, 2020-04-04 06:26:10)) at /Users/svd/code/stillfinder-net/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669)

my Nova/Post.php fields method:

 public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            Text::make('Title'),
            Text::make('Excerpt'),
            Trix::make('Body')
        ];
    }

my Posts migration:

public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('slug')->unique();
            $table->string('title');
            $table->text('excerpt');
            $table->text('body');
            $table->unsignedBigInteger('user_id');
            $table->softDeletes();
            $table->timestamps();
        });
    }

Appreciate any help

0 likes
2 replies
bobbybouwmann's avatar
Level 88

According to your database structure, a post always need to have a user_id. You need to add this field to Nova as well.

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),

        BelongsTo::make('User'), // Add this field

        Text::make('Title'),
        Text::make('Excerpt'),
        Trix::make('Body'),
    ];
}

Please or to participate in this conversation.