如何在rails中加入没有主键的表? [英] How to join tables with no primary key in rails?

查看:50
本文介绍了如何在rails中加入没有主键的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个没有主键的表.下面是两个表结构.

I have two tables that both have no primary key. below is the two tables structure.

如何在 rails 列日期的连接表中使用它?

How can I use this in a join table on column date in rails ?

与此同时,日期列在两个表中都不是唯一的.

Meanwhile, Date columns are not unique in both tables.

推荐答案

根据我的理解(可能是错误的),你需要使用一个主键,以便在 Rails 中识别外键

From my understanding (which may be wrong), you need to use a Primary key, in order to identify foreign keys in Rails

外键

但是,您可以尝试在关联定义中使用 association_foreign_keyforeign_key 参数(仅适用于 HABTM):

However, you could try using the association_foreign_key and foreign_key arguments in your associations definitions (only works for 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

在表格中包含 id 列会如此困难吗??

Would be so difficult to include an id column in your tables??

这篇关于如何在rails中加入没有主键的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆