我刚开始学习 Rails,如果这个问题的答案很明显,我很抱歉。
我已经在我的应用程序中添加了对帖子和类别表的迁移,现在我使用以下行在我的帖子表中添加对默认值非空的类别的引用:
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, :category, index: true, foreign_key: true, null: false
change_column :posts, :category_id, :integer, null: false
关于ruby-on-rails - Rails4 迁移-添加 null : false to an add_reference method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32305821/