Make column not nullable in a Laravel migration

Make column not nullable in a Laravel migration

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

Stefan Bogdanescu

Founder & Senior Architect

Handling Nullable Constraints in Laravel Migrations

Laravel's schema builder makes altering column definitions straightforward, but there is a specific asymmetry when dealing with nullable constraints that often trips up developers. When you need to make a column nullable during an upgrade, you use nullable(true). However, reversing this change in the down method requires a different approach than simply calling nullable(false) or omitting the argument entirely.

To understand why the syntax differs, it helps to look at how the underlying database engine processes these constraints. In most relational databases, including MySQL and PostgreSQL, a column definition for nullability is tied strictly to whether the NULL keyword appears in the ALTER TABLE statement. If you execute ALTER TABLE users MODIFY name VARCHAR(255) NULL, the database permits null values. Conversely, executing ALTER TABLE users MODIFY name VARCHAR(255) NOT NULL enforces the constraint.

If you were to simply omit the NULL keyword in your migration's down function, many drivers would default the column to allowing nulls again, effectively undoing your intended restriction. This behavior is a legacy quirk of how SQL interprets implicit defaults for modifiers. To ensure the constraint is strictly enforced during the downgrade, you must explicitly specify notNullable().

Consider the following migration snippet where we are adding a timestamp field that is currently nullable:

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

return new class extends Migration {
    public function up(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->timestamp('email_verified_at')->nullable();
        });
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->timestamp('email_verified_at')->notNullable();
        });
    }
};

In the up method, nullable() is used to allow the database to store null values for that column. In the down method, omitting the argument or using change() without specifying the constraint would result in the column being nullable again. By calling notNullable(), you explicitly instruct the schema builder to generate an ALTER TABLE ... MODIFY ... NOT NULL statement behind the scenes.

This explicit requirement is particularly important when dealing with existing data. If a table currently contains null values for that column, attempting to make it non-nullable without first handling the existing rows will cause the migration to fail with a constraint violation error. The database engine will reject any row where the value is null because the schema definition now forbids it.

Before running a migration that enforces notNullable(), you must ensure that every row in the table has a valid, non-null value for that column. If your application logic allows for nulls temporarily (for instance, during a data import or a feature flag rollout), you might need to seed default values or handle the transition carefully.

If you are working with an older version of Laravel where the notNullable() helper was not immediately available in all contexts, you might encounter issues where the down method does nothing if the column was already nullable in the initial state of your application. However, once a migration has been run and the table structure is updated, the reversal logic becomes mandatory to maintain consistency between your environment's schema and its data integrity rules.

For more details on managing schema changes and ensuring your migrations are reversible without losing data integrity, you can refer to the comprehensive database schema documentation available at https://laravelcompany.com.

Our network