![](/img/trans.png)
[英]How to make Ruby on Rails create SQLite database with foreign keys enabled?
[英]Ruby on rails does not create foreign keys on database
Rails不在数据库上创建外键是否正常? 还是我做错了什么?
我有以下模型:
class City < ActiveRecord::Base
has_many :users
end
class User < ActiveRecord::Base
belongs_to :city
end
及其各自的迁移:
class CreateCities < ActiveRecord::Migration
def change
create_table :cities do |t|
t.string :name
t.timestamps
end
end
end
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.references :city, index: true
t.timestamps
end
end
end
没错 Rails不会自动添加外键,您必须像在迁移中一样自行指定外键。
t.references :city
实际上与t.integer :city_id
相同。
您是说即使指定了外键,也看不到结果?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.