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

ruby-on-rails – Rails4 migration-在add_reference方法中添加n

发布时间:2020-12-17 02:24:38 所属栏目:百科 来源:网络整理
导读:我刚刚开始学习rails,如果对此的回答相当明显,那就很抱歉. 我在我的应用程序中添加了帖子和类别表的迁移,现在使用以下行添加对posts表中类别的引用,其默认值为not null: add_reference :posts,:category,index: true,foreign_key: true,null: false 但是我
我刚刚开始学习rails,如果对此的回答相当明显,那就很抱歉.

我在我的应用程序中添加了帖子和类别表的迁移,现在使用以下行添加对posts表中类别的引用,其默认值为not null:

add_reference :posts,:category,index: true,foreign_key: true,null: false

但是我在运行迁移时遇到以下错误:

SQLite3::SQLException: Cannot add a NOT NULL column with default value NULL: ALTER TABLE "posts" ADD "category_id" integer NOT NULL

我试过通过api阅读,但无法弄清楚我做错了什么.

解决方法

也许sqlite3不允许这样做,
尝试

add_reference :posts,null: false
change_column :posts,:category_id,:integer,null: false

(编辑:李大同)

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

    推荐文章
      热点阅读