Laravel migration with SQLite 'Cannot add a NOT NULL column with default value NULL'

前端 未结 8 723
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 23:50

Why am I getting this warning when using the SQLite driver? I have no problems with the MySQL driver but SQLite is throwing this error.

It does not make sense to me

8条回答
  •  一整个雨季
    2020-12-06 00:15

    Another work around for this issue is to first create the fields as nullable and then later change the fields to be not null. So for example in this case we will do something like the following:

    public function up() {
        // Create fields first as nullable
        Schema::table('users', function(Blueprint $table) {
            $table->date('birthday')->after('id')->nullable();
            $table->string('last_name')->after('id')->nullable();
            $table->string('first_name')->after('id')->nullable();
        });
    
        // Either truncate existing records or assign a value to new fields
        if (true) {
            DB::table('users')->truncate();
        } else {
            DB::table('users')->update([
                'birthday' => '2019-05-01',
                'last_name' => 'last name',
                'first_name' => 'first name',
            ]);
        }
    
        // Change the fields to not be null
        Schema::table('users', function(Blueprint $table) {
            $table->date('birthday')->nullable(false)->change();
            $table->string('last_name')->nullable(false)->change();
            $table->string('first_name')->nullable(false)->change();
        });
    }
    

提交回复
热议问题