简体   繁体   English

如何在Rails中联接没有主键的表?

[英]How to join tables with no primary key in rails?

I have two tables that both have no primary key. 我有两个都没有主键的表。 below is the two tables structure. 下面是两个表的结构。

在此处输入图片说明

How can I use this in a join table on column date in rails ? 如何在Rails列日期的联接表中使用它?

Meanwhile, Date columns are not unique in both tables. 同时,日期列在两个表中都不都是唯一的。

From my understanding (which may be wrong), you need to use a Primary key, in order to identify foreign keys in Rails 据我了解(可能是错误的),您需要使用主键,以便在Rails中标识外键


Foreign Keys 外键

However, you could try using the association_foreign_key and foreign_key arguments in your associations definitions ( only works for HABTM ): 但是,您可以尝试在关联定义中使用association_foreign_keyforeign_key参数( 仅适用于HABTM ):

#app/models/table_a.rb
Class TableA < ActiveRecord::Base
    has_and_belongs_to_many :table_b, foreign_key: "country_id", association_foreign_key: "hour"
end

#app/models/table_b.rb
Class TableA < ActiveRecord::Base
    has_and_belongs_to_many :table_a, foreign_key: "hour", association_foreign_key: "country_id" 
end

table_as_table_bs
hour | country_id

Would be so difficult to include an id column in your tables?? 在表中包含id列会如此困难吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM