Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Djomobil's avatar

Phpunit + SQLite General error: 1 Cannot add a NOT NULL column with default value NULL

i've this error when i try my test. but working when the application is live.

0 likes
5 replies
rss's avatar

How does your related migration file look like? As the error says, if you have a NOT NULL column it shouldn't have default value NULL.

Djomobil's avatar

my Error : (SQL: alter table "domains" add column "registrar_id" integer not null) my migration :

public function up()
    {
        Schema::table('domains', function (Blueprint $table) {
            $table->unsignedBigInteger('registrar_id');
            $table->foreign('registrar_id')->references('id')->on('registrars');
        });
    }
nbj's avatar
nbj
Best Answer
Level 14

@djomobil This might just be SQLite being a little picky. I just tried this out in a fresh laravel project and get the exact same error running tests.

What I assuming goes wrong is that SQLite will not agree to do an ALTER TABLE where you add a column which is either not nullable or has no default value, this might be some protection in regards to a table already having data and in that case, it would not know what to do.

You have 3 solutions:

  1. Add your column and constraint to the create_domains_table migration
  2. Make registrar_id nullable
  3. Give registrar_id a default value
Djomobil's avatar

i solved my problem with :

Schema::table('domains', function (Blueprint $table) {
            $table->unsignedBigInteger('registrar_id')->nullable()
            $table->foreign('registrar_id')->references('id')->on('registrars');
        });

Thk for helping !

Please or to participate in this conversation.