General error: 1364 Field 'uuid' doesn't have a default value
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Solving the Mystery: General Error 1364 - Field 'uuid' doesn't have a default value in Laravel
As a senior developer working with Eloquent and database migrations, we all encounter those frustrating runtime errors. One that often pops up when dealing with UUIDs—General error: 1364 Field 'uuid' doesn't have a default value—is particularly maddening. It signals a fundamental mismatch between what your application expects to save and what the underlying database schema allows.
I’ve spent time debugging this exact scenario, and I can tell you that while seeding data often works fine, direct creation or mass assignment frequently fails. Today, we will dissect why this happens in a Laravel context and implement robust solutions to ensure your data integrity is maintained.
Understanding the Error: Why Does This Happen?
The error Field 'uuid' doesn't have a default value occurs when you attempt to insert a new record into a database table where a specific column exists, but you have not provided a value for that column, and crucially, the database schema does not define a default value for it.
In your case, this is happening because:
- The Migration: Your
CreateProjectsTablemigration defines theuuidcolumn but omits anydefault()constraint:$table->uuid('uuid')->index(); // No default set - Eloquent/Mass Assignment: When you call
$project::create($request->all()), if the incoming request data lacks a value foruuid, Eloquent attempts to insertNULL(or an empty string, depending on configuration), which is rejected by the database because the column requires a non-null value and has no fallback.
The reason seeding works is that your Factory explicitly generates a UUID using $this->faker->uuid(), providing the necessary data upfront, bypassing the strict requirement for a default value during that specific operation. But this doesn't solve the problem for general application logic.
Three Practical Solutions for UUID Management
To resolve this and ensure your application handles UUIDs reliably, we need to define where the UUID originates: either in the application layer (Eloquent) or the database layer (Migration).
Solution 1: Setting a Database Default Value (The Safest Approach)
If you want the database itself to handle the generation of IDs upon insertion—which is often preferred for primary keys and unique identifiers—you should set a default value directly in the migration.
For UUIDs, depending on your specific database (PostgreSQL, MySQL, etc.), you can use database-specific functions within the default() method. For PostgreSQL, this is very straightforward:
// In your CreateProjectsTable migration
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->id();
// Use a default UUID generation function if supported by your DB
$table->uuid('uuid')->default(DB::raw('gen_random_uuid()')); // Example for PostgreSQL
$table->string('name');
// ... rest of the table
});
}
Note: Always verify the correct UUID generation syntax for your specific database system. This approach delegates the responsibility to the database, making your application code cleaner.
Solution 2: Ensuring Data Generation in Factories (The Seeding Fix)
Since you are using factories, ensure that every factory that creates a model explicitly defines the value for all required fields. Your current factory implementation is good because it generates the data:
// In ProjectFactory
public function definition()
{
$status = ProjectStatus::getRandomValue();
return [
'uuid' => $this->faker->uuid(), // Explicitly setting the value
'user_id' => User::factory(),
'name' => $this->faker->word(),
'status' => $status,
];
}
This confirms that when seeding, the data is present. However, this method only solves the issue for factories; it doesn't fix how a user-submitted form might try to create a record without providing the ID.
Solution 3: Using Mutators and Casting (The Eloquent Approach)
For advanced control over UUIDs, especially when dealing with external systems or ensuring consistency across all model interactions, you can use Eloquent Mutators to automatically generate the value before saving, ensuring the database always receives a valid string.
While this is more complex, it enforces data integrity at the application level, which aligns perfectly with the robust structure Laravel promotes—think about how powerful Eloquent features are when you leverage them fully (as seen on the official Laravel Company documentation).
Conclusion
The error Field 'uuid' doesn't have a default value is a classic symptom of missing constraints in your database schema when dealing with mandatory fields. By analyzing whether the data should be generated by the database, the factory, or Eloquent itself, you can apply the appropriate fix. For production applications, defining defaults at the migration level (Solution 1) provides the most robust and least error-prone foundation for your entire application. Always strive for consistency between your database structure and your application logic to write cleaner, more maintainable code.