Elixir 如何避免Ecto向上的引用添加前缀有很多关系
我有以下模式:Elixir 如何避免Ecto向上的引用添加前缀有很多关系,elixir,ecto,Elixir,Ecto,我有以下模式: schema "products" do field :product_id, :integer field :title, :string has_many :variants, MyApp.Variant, references: :product_id timestamps end schema "variants" do field :variant_id, :integer field :title, :string
schema "products" do
field :product_id, :integer
field :title, :string
has_many :variants, MyApp.Variant, references: :product_id
timestamps
end
schema "variants" do
field :variant_id, :integer
field :title, :string
belongs_to :product, MyApp.Product
timestamps()
end
我想通过使用product表中的product_id字段而不是默认id字段将变体链接到product
我遇到的问题是,当我查询产品并预加载它的变体时,出现了指向以下内容的错误:
from v in MyApp.Variant,
where: v.product_product_id in ^[198977],
order_by: [asc: v.product_product_id],
select: {v.product_product_id, v}
Exto认为变体表中的外键是product\u product\u id。Exto添加前缀product\u
我如何阻止Ecto添加前缀?前缀仅适用于id字段
由于某些原因,我无法使用products表中的id字段链接到variants表。我必须使用不同的字段
谢谢。尝试将架构“变体”中的所属更改为: 此外,要对架构中的主键使用不同的字段名,您需要使用
@primary\u key
传递元组{field\u name,type,options}
,例如:
@primary_key {:product_id, :id, autogenerate: true}
让我知道这是否有帮助
@primary_key {:product_id, :id, autogenerate: true}