Laravel迁移与sqllite'无法添加NOT NULL列与默认值NULL' [英] Laravel migration with sqllite 'Cannot add a NOT NULL column with default value NULL'

查看:693
本文介绍了Laravel迁移与sqllite'无法添加NOT NULL列与默认值NULL'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在使用SQLLITE驱动程序时收到此警告?我没有MySQL驱动程序的问题,但SQLLITE正在抛出此错误。



对我来说没有意义,因为我明白所有的迁移完成之后发生种子为什么它抱怨这个问题,只有数据已经​​存在于数据库中才会出现。



我的两次迁移是



FIRST MIGRATION

  public function up(){
Schema :: create('users',function($ table){
$ table-> increment('id');
$ table-> string('username');
$ table - > string('email');
$ table-> string('password');
});
}

SECOND MIGRATION

  public function up(){
Schema :: table('users',function(Blueprint $ table){
$ table- > date('birthday') - > after('id');
$ table-> string('last_name') - > after('id');
$ table- > string('first_name') - >之后('id');
});
}

错误

 异常:SQLSTATE [HY000]:常规错误:1无法添加默认值为NULL的NOT NULL列(SQL:alter tableusersadd columnbirthday日期不为空)


解决方案

看起来这是一个SQLite怪胎。根据关于同一问题的Laracast论坛主题



从头开始添加表时,可以指定NOT NULL。但是,添加列时不能执行此操作。 SQLite的规范说你必须有一个默认值,这是一个不好的选择。



进一步说明了 SQLite ALTER TABLE docs ,我发现:



如果指定了NOT NULL约束,则列必须具有默认值NULL。



我认为在SQLite的世界里,没有提供默认值是一样的说,默认值应该为NULL(而不是这个不可为空的列没有默认值,因此在每个插入上都必须为它提供一个值)。



如果您需要向现有表添加不可空列,似乎SQLite会使您处于不良状态,该列也不应具有默认值。


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

It does not make sense to me since I understood the seeding happens after all the migrations are completed so why is it complaining about this issue which would only arise if data was already present in the database.

My two migrations are

FIRST MIGRATION

  public function up() {
    Schema::create('users', function($table) {
      $table->increments('id');
      $table->string('username');
      $table->string('email');
      $table->string('password');
    });
  } 

SECOND MIGRATION

public function up() {
    Schema::table('users', function(Blueprint $table) {
        $table->date('birthday')->after('id');
        $table->string('last_name')->after('id');
        $table->string('first_name')->after('id');
    });
}

ERROR

Exception: SQLSTATE[HY000]: General error: 1 Cannot add a NOT NULL column with default value NULL (SQL: alter table "users" add column "birthday" date not null)

解决方案

It looks like this is a SQLite oddity. According to a Laracast forum thread about the same issue:

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.

Looking further to the SQLite ALTER TABLE docs, I found:

If a NOT NULL constraint is specified, then the column must have a default value other than NULL.

I suppose in the world of SQLite, not providing a default value is the same thing as saying the default value should be NULL (as opposed to meaning there is no default value for this non-nullable column, so a value must be provided for it on each insert).

It seems SQLite simply leaves you in a bad state if you need to add a non-nullable column to an existing table, which column should also not have a default value.

这篇关于Laravel迁移与sqllite'无法添加NOT NULL列与默认值NULL'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆