gpt4 book ai didi

mysql - 如何在 Rails 中连接没有主键的表?

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:22 29 4
gpt4 key购买 nike

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

enter image description here

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

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

最佳答案

根据我的理解(可能是错误的),您需要使用主键来识别 Rails 中的外键


外键

但是,您可以尝试在关联定义 (only works for HABTM) 中使用 association_foreign_keyforeign_key 参数:

#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 列会这么难吗??

关于mysql - 如何在 Rails 中连接没有主键的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22216758/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com