Rails:'references:foo'和'integer:foo_id'之间有区别吗? [英] Rails: is there a difference between 'references :foo' and 'integer :foo_id'?
问题描述
当我在迁移中使用references :foo
时,生成的列称为foo_id
.实际上,执行references :foo
和仅执行integer :foo_id
有什么区别吗?也许是在后台执行某些事情以在数据库级别强制执行该关系?
When I use references :foo
in a migration, the column that's generated is called foo_id
. Is there actually any difference between doing references :foo
and just doing integer :foo_id
? Maybe something going on under the hood to enforce the relationship at the database level?
推荐答案
对于您的特定情况,结果是相同的;你是对的.但是references
允许使用:polymorphic => true
选项,该选项将自动在表中将foo_type
列创建为字符串.
The result is the same for your specific case; you are correct. But references
allows for a :polymorphic => true
option which will automatically create the foo_type
column as a string in the table.
从语义上讲,如果尝试使迁移更好地反映数据库中表之间的关系,则references
会更好.
Semantically, references
is better if you are trying make your migrations better reflect the relations between tables in the database.
这篇关于Rails:'references:foo'和'integer:foo_id'之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!