fenos
4 years ago
189
10
General

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

Posted 4 years ago by fenos

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 :)

Please sign in or create an account to participate in this conversation.