我正在为我的应用重构代码。
我决定在Elixir中使用|>
来改变查询功能的样式。
我有一个查询功能,可以通过加入两个不同的表sales_items
和Item
来查询Category
。它工作正常。
from(p in SalesItem, join: i in Item, on: p.item_id == i.id,
join: ca in Category, on: i.category_id == ca.id,
where: p.sales_id == ^sales_id,
order_by: [desc: p.id]) |> Repo.all()
我使用管道操作符更改了上面的功能。
SalesItem
|> join(:inner, [s], i in Item, s.item_id == i.id)
|> join(:inner, [s, i], ca in Category, i.category_id == ca.id)
|> where([s, i, ca], s.sales_id == ^sales_id)
|> order_by([s, i, ca], [desc: s.id])
|> Repo.all()
但是,我使用管道运算符编写的第二个函数触发了(undefined_column): column c1.category_id does not exist
如何解决此问题?
- 编辑,添加架构
schema "sales_items" do
field :item_total, :float, default: 0.0
field :price, :integer
field :quantity, :integer
field :sales_id, Ecto.UUID
field :item_id, Ecto.UUID
field :user_id, Ecto.UUID
field :has_attrs, :boolean, default: false
timestamps()
end
schema "items" do
field :active, :boolean, default: true
field :deleted, :boolean, default: false
field :price, :integer
field :category_id, Ecto.UUID
field :subcategory_id, Ecto.UUID
field :sort_id, :integer
timestamps()
end
schema "categories" do
field :subcategory, :boolean, default: false
field :sort_id, :integer
field :active, :boolean, default: true
timestamps()
end