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
答案 0 :(得分:0)
那是对的。 Rails不会自动添加外键,您必须自己在迁移中自行指定外键。
t.references :city
实际上与t.integer :city_id
相同。
你是说虽然指定了外键,但你没有看到结果?