Laravel 5.4 field doesn't have a default value

Laravel 5.4 field doesn't have a default value

  1. Blog
  2. 15 November 2026
  3. 5 min read

Stefan Bogdanescu

Founder & Senior Architect

Why user_id Is Missing When Creating a Match via Relationship

The error message SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value is a classic symptom of a mismatch between your database schema and the data you are attempting to insert. In your specific case, the root cause lies in how Laravel handles mass assignment when using relationship methods like create(), combined with a misunderstanding of which fields are actually being sent to the database.

You have defined $guarded = [] on your Match model. This setting tells Laravel to accept any attribute passed in the array during creation as long as it is explicitly whitelisted by fillable or if no fillable restrictions exist (depending on version and configuration, but effectively allowing mass assignment here). However, the error indicates that the user_id field is missing from the insert statement entirely.

Look closely at your creation logic:

$deal->matches()->create(['user_id' => $id]);

You are explicitly passing 'user_id' in the array. If the database column exists and does not have a default value, Laravel must receive that value. The fact that you are getting an error suggests one of two things: either the value $id is null (which shouldn't happen if it's an integer), or—more likely given the SQL output showing only deal_id in the parenthesis—the relationship mapping is preventing the column from being touched, or there is a strict type mismatch causing the value to be ignored.

However, the most probable culprit in this specific scenario involves the timestamps = false property and how it interacts with your custom boot logic. You have set public $timestamps = false;. This tells Laravel not to touch the created_at or updated_at columns automatically. While this is a valid configuration for legacy databases, it often leads developers to manually handle every single column they want to insert.

Your boot() method attempts to set $model->matched_on:

static::creating(function ($model) {
    $model->matched_on = $model->freshTimestamp();
});

This runs before the query executes. It sets the attribute on the model instance. Your constructor logic then calculates expired_on based on that timestamp:

public function __construct(){
    $d = (new \DateTime($this->matched_on))->modify('+1 day');
    $this->expired_on = $d->format('Y-m-d H:i:s');
}

Here is where the logic breaks down during mass assignment. When you call create(['user_id' => $id]), Laravel instantiates the model, sets the $attributes array with ['user_id' => $id], and then calls your constructor. Inside the constructor, you try to access $this->matched_on. At this exact moment, matched_on has not been set yet because the creating event hasn't fired—the model is still being constructed. Consequently, $model->freshTimestamp() in the boot method runs on a fresh model instance, but more critically, if your constructor relies on $this->matched_on before it exists, you might be injecting an empty string or causing a logic flow that skips setting user_id.

Wait, let's re-examine the SQL error: insert into matches (deal_id) values (1). The SQL explicitly shows that only deal_id is being inserted. Your relationship definition includes $this->belongsTo('App\Deal'). In Laravel 5.4 and similar versions, when you use $parent->relationship()->create(), the parent key (deal_id) is automatically populated by Laravel based on the foreign key constraint defined in your migration or the relationship method's foreignPivotKey (which defaults to id if not specified, but usually matches the column name).

The issue is that you are passing 'user_id' manually, yet it is not appearing in the SQL. This happens if the $guarded = [] setting is overridden by a strict fillable property that you might have missed or if the value being passed is not recognized. But there is a simpler explanation often overlooked: The relationship definition itself.

If your Match model has public $timestamps = false, ensure that you are not accidentally relying on automatic fillable lists. In Laravel 5.4, mass assignment was somewhat strict. Even with $guarded = [], if you do not define a fillable property, the default behavior depends on the strict mode of the connection or specific model flags.

More importantly, check your database migration for the matches table. The error states Field 'user_id' doesn't have a default value. This confirms the column exists and requires data. Since you are passing it in the array, why is it missing?

The answer lies in the relationship definition. You defined:

public function deal()
{
    return $this->belongsTo('App\Deal');
}

If your deal table uses snake_case for the ID (standard Laravel convention), the foreign key on the matches table should be named deal_id. The SQL output confirms this: (deal_id).

However, you are passing user_id. If your database column is actually named something else, like app_user_id or if the relationship mapping is expecting a specific attribute name that doesn't match the column, Laravel might skip it. But assuming the column name is exactly user_id, the issue is likely in how the value is being handled.

A common pitfall in this architecture is the interaction between the constructor and the boot event. Your constructor modifies $this->expired_on based on $this->matched_on. If $matched_on is not set when the constructor runs, freshTimestamp() might return a valid time, but if the logic expects a specific state that isn't met, it could fail silently or alter the attribute list.

To fix this reliably, ensure your migration defines the foreign keys correctly and that you are passing the ID as an integer. Also, verify that $guarded = [] is indeed the only protection layer. In Laravel 5.4, it is often safer to explicitly define a fillable array even if guarded is empty, to avoid ambiguity:

protected $fillable = ['user_id', 'deal_id', 'matched_on', 'expired_on'];

By explicitly listing user_id, you guarantee that Laravel treats it as a mass-assignable column. If your database allows NULLs for other columns but not user_id, ensure no other logic is overriding the passed value.

Finally, consider if the $id variable you are passing is actually an integer. Passing a string representation of an ID can sometimes cause issues with strict SQL modes or foreign key constraints depending on the driver configuration. Casting it to (int) before insertion ensures type safety:

$deal->matches()->create(['user_id' => (int) $id]);

If you continue to face this issue, double-check your migration file for the matches table to ensure there are no unexpected constraints or if the column was created with a different name than expected. Sometimes, renaming the column in the migration doesn't update the relationship foreign key name automatically, leading to Laravel ignoring the passed attribute because it doesn't match the defined foreign key mapping.

Our network