Rails:我可以使用带有非整数主键的多态引用吗?
我有一个使用 UUID 作为主键的数据库,如下所示:
create_table "my_table", :id => false, :force => true do |t|
t.string "id", :limit => 36
end
但是,当我尝试使用 :references 作为该表的外键时,它会生成 ID 的整数列。是否可以指示 :references 处理非整数 ID?我对引用表的迁移是这样的:
create_table "child_table" :id => false, :force => true do |t|
t.string "id", :limit => 36
t.references :my_table
end
我知道我可以手动创建 :my_table_id
和 :my_table_type
列,但我想知道 :references 是否
可以在这些情况下发挥其魔力,这样我就不必在整个代码中显式处理 id+type 了。
I have a database that uses UUIDs as primary keys, like this:
create_table "my_table", :id => false, :force => true do |t|
t.string "id", :limit => 36
end
However, when I try to use :references for foreign keys to that table, it generates integer columns for the ID. Can :references be instructed to deal with a non-integer ID? My migration for the referring table is like this:
create_table "child_table" :id => false, :force => true do |t|
t.string "id", :limit => 36
t.references :my_table
end
I know that I could just manually create :my_table_id
and :my_table_type
columns, but I'm wondering whether :references
can be made to do its magic under these circumstances so that I don't have to handle the id+type explicitly throughout my code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

从 Rails 4.2 开始,引用时添加了 :type 选项
例如:
source: https://github.com/rails/rails /拉/16231
A :type option has been added when referencing since Rails 4.2
For example:
source: https://github.com/rails/rails/pull/16231