加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite – 使用sqllite的Laravel迁移’无法添加一个NOT NULL列,

发布时间:2020-12-12 19:04:01 所属栏目:百科 来源:网络整理
导读:为什么在使用SQLLITE驱动程序时收到此警告?我没有 MySQL驱动程序的问题,但是SQLLITE抛出了这个错误. 这对我来说没有意义,因为我理解种子在所有的迁移完成后发生,所以为什么它抱怨这个问题,只有数据已经??存在于数据库中才会出现. 我的两个迁移是 第一次移民
为什么在使用SQLLITE驱动程序时收到此警告?我没有 MySQL驱动程序的问题,但是SQLLITE抛出了这个错误.

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

我的两个迁移是

第一次移民

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

第二次移民

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');
    });
}

错误

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)
看起来这是一个SQLite的奇怪.根据 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.

进一步看看SQLite ALTER TABLE docs,我发现:

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

我认为在SQLite的世界里,不提供一个默认值就是说默认值应该是NULL(而不是这个不可空的列没有默认值,所以必须为它提供一个值)在每个插入).

如果您需要向现有表添加不可空列,那么SQLite会让您处于不良状态,哪个列也不应该具有默认值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读