Field 'id' doesn't have a default value for UUID Field in Laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Field 'id' doesn't have a default value for UUID Field in Laravel: Migrating from CakePHP to Eloquent IDs

Welcome to the world of Laravel! If you are familiar with frameworks like CakePHP, you likely understand how database schema management works. However, migrating patterns—especially around primary keys and data types—often reveals subtle but critical differences between systems. Today, we are diving into a very common stumbling block when adopting UUIDs in Laravel migrations: the issue of missing default values for primary keys.

I’ve seen developers face this exact scenario when transitioning from traditional auto-incrementing integer IDs to universally unique identifiers (UUIDs). Let's break down why this error occurs and how we can architect our database schema correctly in Laravel.

The Root Cause: Missing Default Values

You are encountering the error SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value because of how relational databases enforce constraints during data insertion.

When you define a column as a primary key, the database expects every row inserted into that table to provide a value for that column. If you simply define the column type (like uuid('id')) without specifying a default value, the database doesn't know what value to insert when Laravel tries to execute an INSERT statement for a new record. Unlike auto-incrementing integers (which rely on the database engine to generate the next sequential number), UUIDs require an explicit mechanism to generate that unique value upon insertion.

In your provided migration:

$table->uuid('id');
$table->primary('id');

You defined the column type and constraint, but you didn't tell the database how to populate the id column for new entries.

The Laravel Solution: Leveraging Database Functions

The solution lies in instructing the database itself to generate the UUID when a row is created. This shifts the responsibility of ID generation from relying on application logic (which can fail) to the robust capabilities of the underlying database system.

For modern PostgreSQL databases, which are highly favored in the Laravel ecosystem, you can utilize built-in functions for generating UUIDs directly within the migration.

Corrected Migration Example

Instead of just defining the column, we explicitly set a default value using the appropriate database function:

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            // Use ->uuid() or define a default value using DB::raw() depending on your setup
            $table->uuid('id')->primary(); // Define the UUID column and set it as primary
            
            // For PostgreSQL, you can also explicitly use the gen_random_uuid function if needed:
            // $table->uuid('id')->default(DB::raw('gen_random_uuid()')); 

            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

        // ... rest of the migration
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

A Note on Database Dialects: While uuid() is a convenient method provided by Laravel's Schema builder, for maximum portability and control over database-specific functions (like PostgreSQL’s gen_random_uuid()), using the DB::raw() method gives you explicit control. This practice is crucial when dealing with complex or cross-database applications, aligning with best practices discussed on platforms like laravelcompany.com.

Best Practices for UUID in Laravel

When managing primary keys in Laravel, keep these principles in mind:

  1. Choose the Right Type: For distributed or large-scale systems, UUIDs are excellent. Ensure your database supports native UUID types (like UUID in PostgreSQL) rather than storing them as strings if possible, as this improves indexing and performance.
  2. Use Eloquent Casting: Once the migration is correct, ensure your Eloquent models are set up to handle these IDs correctly by casting the attribute to a string or UUID type. This makes data handling much cleaner in your application code.
  3. Migration First: Always define the structure and constraints in your migrations first. The database layer should dictate how data is stored before the application layer tries to interact with it.

Conclusion

The error you encountered is a classic example of the gap between defining what a column is and defining how that column is populated. By explicitly setting a default value—either via native database functions or by ensuring Laravel's schema builder correctly maps the UUID generation mechanism—you resolve this conflict. Mastering these database details is what separates functional code from robust, production-ready applications. Keep building amazing things with Laravel!