通过ActiveRecord迁移创建具有外键约束的SQLite 3表
与MySQL和Postgres不同,SQLite不支持向现有表中添加外键约束。因此,不能在ActiveRecord迁移中使用通过ActiveRecord迁移创建具有外键约束的SQLite 3表,activerecord,sqlite,foreign-keys,rails-migrations,Activerecord,Sqlite,Foreign Keys,Rails Migrations,与MySQL和Postgres不同,SQLite不支持向现有表中添加外键约束。因此,不能在ActiveRecord迁移中使用add_foreign_key创建外键约束 但是,从3.6.19开始,SQLite在CREATE TABLE语句中包含外键约束,并且在定义CREATE_TABLE迁移中的reference列时指定foreign_key:true: class CreateChildren < ActiveRecord::Migration def change creat
add_foreign_key
创建外键约束
但是,从3.6.19开始,SQLite在CREATE TABLE语句中包含外键约束,并且在定义CREATE_TABLE
迁移中的reference
列时指定foreign_key:true
:
class CreateChildren < ActiveRecord::Migration
def change
create_table :children do |t|
t.text :name
t.references :parent, foreign_key: true
end
end
end
这是因为技术原因而无法实现的,还是因为缺少了一个功能?有解决办法吗
create_table "children", force: :cascade do |t|
t.text "name"
t.integer "parent_id"
end