- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下两个模型:
class Store < ActiveRecord::Base
belongs_to :person
end
class Person < ActiveRecord::Base
has_one :store
end
问题是:我正在尝试创建迁移以在 people 表中创建外键。但是,引用 Store 外键的列并未按照 Rails 约定命名为 store_id,而是命名为 foo_bar_store_id。
如果我遵循 Rails 约定,我会像这样进行迁移:
class AddReferencesToPeople < ActiveRecord::Migration
def change
add_reference :people, :store, index: true
end
end
但是,这不起作用,因为列名称不是 store_id,而是 foo_bar_store_id。那么如何指定外键名称只是不同,但仍保持 index: true 以保持快速性能?
最佳答案
在 Rails 5.x 中,您可以向具有不同名称的表添加外键,如下所示:
class AddFooBarStoreToPeople < ActiveRecord::Migration[5.0]
def change
add_reference :people, :foo_bar_store, foreign_key: { to_table: :stores }
end
end
在 create_table
block 内
t.references :feature, foreign_key: {to_table: :product_features}
关于ruby-on-rails - Rails 迁移 : add_reference to Table but Different Column Name For Foreign Key Than Rails Convention,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27809342/
我正在尝试使用类型特征来添加对模板参数的引用。 template struct S { typename add_reference::type reference; // reference me
我正在查看 [VC10 的] unique_ptr,它们做了一些我不明白的事情: typedef typename tr1::remove_reference::type _Dx_noref; _Dx
我有以下迁移 class LinkDoctorsAndSpecializations 'index name' 中指定的那样 最佳答案 作为我commented , 做: add_index :ta
向上迁移这个迁移 def change remove_reference :order_items, :order, foreign_key: true end 或向下迁移此迁移 def ch
rails 中的"add_foreign_key" 和"add_reference" 方法有什么区别? 根据rails official guide,我的理解是它们都用于在两个表之间创建外键约束。 最
在 Rails 5 和 PostgreSQL 中。 我创建这个文件是为了默认使用 bigint 数据类型而不是 integer: # config/initializers/bigint_primar
我刚开始学习 Rails,如果这个问题的答案很明显,我很抱歉。 我已经在我的应用程序中添加了对帖子和类别表的迁移,现在我使用以下行在我的帖子表中添加对默认值非空的类别的引用: add_referenc
我有以下两个模型: class Store < ActiveRecord::Base belongs_to :person end class Person < ActiveRecord::B
我是一名优秀的程序员,十分优秀!