Error guides

SQLSTATE[HY000]: General error: 1364 Field doesn't have a default value

An insert left out a NOT NULL column that has no default. Pass the value, check that the model lets it through, or make the column nullable or defaulted.

Message
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value
Exception
Illuminate\Database\QueryException

What it means

The insert did not include a value for a column that is NOT NULL and has no DEFAULT, so MySQL refused the row. The message names the column; the SQL in the full message lists the columns the insert did send.

Why it happens

  • The code never sets the column. DB::table('posts')->insert(['title' => 'Hello']) on a table with a required user_id fails this way.
  • The value was passed but Eloquent dropped it. With #[Fillable(['title'])] on the model, Post::create(['user_id' => 1, 'title' => 'Hello']) silently discards user_id, and the insert that reaches MySQL holds only title and the timestamps. The mass assignment guide explains how Eloquent decides.
  • A column was added later without nullable() or default(). The migration itself runs fine: MySQL fills the existing rows with an empty string for a string column, or 0 for an integer. The first insert after the deploy that does not set the new column fails.

MySQL raises this error in strict SQL mode, which Laravel turns on through 'strict' => true in the default config/database.php. Turning it off is not a fix. With 'strict' => false, MySQL stores the type's implicit default instead: 0 for a number, an empty string for text. In the test, the post with the dropped user_id then failed on the foreign key as 1452 Cannot add or update a child row, and a missing title was saved as an empty string without any error.

How to fix it

Set the value. For a parent like the user, create the row through the relationship, which fills the foreign key for you. This assumes a posts() has-many relation on User and a $validated array from your request validation:

$post = $request->user()->posts()->create([
    'title' => $validated['title'],
]);

If the value really comes from the input, list the attribute as fillable on the model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;

#[Fillable(['title', 'category_id'])]
class Post extends Model
{
}

During development, make dropped attributes throw instead of vanishing, in the boot method of AppServiceProvider:

use Illuminate\Database\Eloquent\Model;

Model::preventSilentlyDiscardingAttributes($this->app->isLocal());

If the column may be empty, or has a sensible default, say so in a new migration. Changing a column needs ->change(), which drops every modifier you leave out, so repeat the ones the column should keep:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Schema::table('posts', function (Blueprint $table) {
    $table->string('slug')->nullable()->change();
    $table->string('status')->default('draft')->change();
});

When you add a required column, set it everywhere rows are inserted in the same deploy, or give it a default.

Find it in production

With APP_DEBUG=false the visitor gets a 500 page titled "Server Error", typically on the form or action that creates the record. Laravel reports the exception and writes it at error level to storage/logs/laravel.log. By default the logged message includes the insert SQL with every bound value, which means any personal data in that row reaches the log and any error tracker alike. DB_MASK_BINDINGS=true replaces the values with ?; the column list stays, so the missing column still shows. Laravel reads it only through the mask_bindings_in_exception_messages key of the connection, so if your config/database.php connection has no such key, add 'mask_bindings_in_exception_messages' => env('DB_MASK_BINDINGS', false), to it. Ranetrace's error monitoring records each one with its stack trace and the request it happened on.

Reproduced on Laravel 13.45, PHP 8.5, MySQL 8.0.27, 5 December 2026.

Start monitoring today

Websites go down and code throws errors. Ranetrace tells you when it happens to yours, why it matters and what to do next.

Start your 30-day free trial From $9 a month. No credit card required.