gpt4 book ai didi

sql - Rails has_many :through with custom foreign_key

转载 作者:行者123 更新时间:2023-12-03 20:16:28 25 4
gpt4 key购买 nike

我有以下一组模型:

class Cardstock < ActiveRecord::Base
has_many :color_matches, :primary_key => :hex, :foreign_key => :hex
has_many :palette_colors, :through => :color_matches
end

class ColorMatch < ActiveRecord::Base
belongs_to :palette_color
has_many :cardstocks, :foreign_key => :hex, :primary_key => :hex
end

class PaletteColor < ActiveRecord::Base
has_many :color_matches
has_many :cardstocks, :through => :color_matches
end

调用 Cardstock.last.palette_colors产生以下错误:
ActiveRecord::StatementInvalid: PGError: ERROR:  operator does not exist: character varying = integer
LINE 1: ...".palette_color_id WHERE (("color_matches".hex = 66)) OR...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "palette_colors".* FROM "palette_colors" INNER JOIN "color_matches" ON "palette_colors".id = "color_matches".palette_color_id WHERE (("color_matches".hex = 66)) ORDER BY name ASC

这表明 ActiveRecord 生成的查询使用卡片的 id ( 66 ),它应该使用卡片的十六进制 ( bbbbaf )。在某处,我需要指定 ActiveRecord 以使用 hex列之间加入 cardstockscolor_matches . ActiveRecord 支持吗?

最佳答案

你的人际关系在这里完全不正常。

  • Cardstock 和 ColorMatch 之间的关系应该是 has_and_belongs_to_many双方关系
  • 任何有 has_many relationship 的地方,您需要一个对应的belongs_to对应类中的关系
  • 关于sql - Rails has_many :through with custom foreign_key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6331989/

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