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

fenos's avatar

[migrations] SQLite General error: 1 Cannot add a NOT NULL column with default value NULL

Hi guys, I'm having problems with my migrations and sqlite, let me explain :)

I created a migration that add a column to an existing table. it looks like this:

    public function up()
    {
        Schema::table('organisations', function (Blueprint $table){
                      $table->string('long_name');
                });
    }

    public function down()
    {
        Schema::table('organisations', function (Blueprint $table){
                         $table->dropColumn('long_name');
                });

Running this as a migration in MySQL for actually insert the new column, it worked just fine.

When running the same migration on SQlite (For testing purpose), it throw the following error:

PDOException: SQLSTATE[HY000]: General error: 1 Cannot add a NOT NULL column with default value NULL

I first googled a bit about this issue and came a cross to this Blog post

https://www.alfrednutile.info/posts/64

It say that when you are getting this issue, apply the new field as nullable. I personally, don't want the field to be nullable. :(

There is some workaround for it?

Thanks guys if you have any ideas about this :)

0 likes
11 replies
toniperic's avatar
Level 30

This is (what I would consider) a glitch with SQLite. This error occurs whether there are any records in the table or not.

When adding a table from scratch, you can specify NOT NULL. However, you can't do this when adding a column. SQLite's specification says you have to have a default for this, which is a poor choice.

You could specify the default value I guess, such as

Schema::table('organisations', function (Blueprint $table){
    $table->string('long_name')->default('default_value');
});
17 likes
RoBroBro's avatar

Thanks for pointing this out.

I encountered this problem just now and a workaround would be to add it as a nullable field and directly change it to non-nullable after:

Schema::table('organisations', function (Blueprint $table){
    $table->string('long_name')->nullable();
});

Schema::table('organisations', function (Blueprint $table){
    $table->string('long_name')->nullable(false)->change();
});
15 likes
Bartestro's avatar

I know this question is pretty old. I've had also other problems with tests in sqlite on laravel 5.5 and dusk in particular. Just a thoughtful thought... Changing your db structure to fit into different, especially in this case, testing db engine is definitely not the best practice. So I would drop testing in sqlite at least for now.

1 like
mcardle's avatar

I had the same problem, but was able to fix it by running:

php artisan config:cache

Hope it helps. By the way, I using Laravel 5.5

4 likes
Andrae's avatar

In response to @mcardle

php artisan config:cache 

Can have unintended consequences.

It seems to turn on caching, which might have your test run in the wrong database. After doing this command it ran in my mysql and emptied my local database.

There's a post on it by @bobbybouwmann which can be found here: https://laracasts.com/discuss/channels/testing/laravel-ignores-the-testing-connection

To work around this original error, I just created a mysql db for testing. Created a .env.testing file with APP_ENV=testing and then in PHPunit.xml I changed the env

        <env name="APP_ENV" value="testing"/>
3 likes
ibnuh's avatar

@BARTESTRO - I kinda agreed to this. I will just use the same engine and make up 2 databases

mundiakaluson's avatar

I'm new to laravel and I know this is too late.

But this works on my side: $table->string('location')->default('');

Please or to participate in this conversation.