Elixir 通过关联在另一端查询某事物
我想通过多个关联查询筛选结果。我是否必须使用Elixir来完成这项工作,或者我可以使用EXTO在数据库中完成这项工作 安装程序 priv/repo/seeds.exsElixir 通过关联在另一端查询某事物,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,我想通过多个关联查询筛选结果。我是否必须使用Elixir来完成这项工作,或者我可以使用EXTO在数据库中完成这项工作 安装程序 priv/repo/seeds.exs {:ok, invoice1} = Shop.Accounting.create_invoice(%{number: "1", issued_on: "2017-01-01"}) {:ok, invoice2} = Shop.Accounting.create_invoice(%{number: "2", issued_on: "
{:ok, invoice1} = Shop.Accounting.create_invoice(%{number: "1", issued_on: "2017-01-01"})
{:ok, invoice2} = Shop.Accounting.create_invoice(%{number: "2", issued_on: "2017-01-01"})
{:ok, invoice3} = Shop.Accounting.create_invoice(%{number: "3", issued_on: "2017-01-02"})
{:ok, banana} = Shop.Accounting.create_product(%{name: "Banana", category: "Fruits"})
{:ok, potato} = Shop.Accounting.create_product(%{name: "Potato", category: "Vegetables"})
Shop.Accounting.create_line_item(%{invoice_id: invoice1.id, product_id: banana.id})
Shop.Accounting.create_line_item(%{invoice_id: invoice2.id, product_id: banana.id})
Shop.Accounting.create_line_item(%{invoice_id: invoice2.id, product_id: potato.id})
Shop.Accounting.create_line_item(%{invoice_id: invoice3.id, product_id: potato.id})
模式
lib/shop/accounting/invoice.ex
schema "invoices" do
field :issued_on, :date
field :number, :string
has_many :line_items, Shop.Accounting.LineItem
has_many :products, through: [:line_items, :product]
timestamps()
end
schema "line_items" do
belongs_to :invoice, Shop.Accounting.Invoice
belongs_to :product, Shop.Accounting.Product
timestamps()
end
lib/shop/accounting/line_item.ex
schema "invoices" do
field :issued_on, :date
field :number, :string
has_many :line_items, Shop.Accounting.LineItem
has_many :products, through: [:line_items, :product]
timestamps()
end
schema "line_items" do
belongs_to :invoice, Shop.Accounting.Invoice
belongs_to :product, Shop.Accounting.Product
timestamps()
end
查询预加载蔬菜的发票
如何查询从1月1日到1月5日的所有发票
,包括产品
,这些产品的值为蔬菜
,属于类别
。
如果他们也有水果的行项目
,也可以。我只是想确定没有只卖水果而不卖蔬菜的发票。有了这些种子,它将成为发票2和发票3
我知道如何使用Elixir过滤发票。但我想知道我是否能用EXTO更快地在数据库中解决这个问题。有没有一种方法可以用EXTO过滤
import Ecto.Query
alias Shop.Accounting.Invoice
alias Shop.Repo
{:ok, starts_on} = Date.from_erl({2017, 1, 1})
{:ok, ends_on} = Date.from_erl({2017, 1, 5})
query = from i in Invoice, where: i.issued_on >= ^starts_on,
where: i.issued_on <= ^ends_on,
preload: [:products]
invoices = Repo.all(query)
# Here I would loop through invoices to filter the once
# with vegetables.
导入外部查询
别名Shop.Accounting.Invoice
别名商店
{:好的,从}=Date.from{2017,1,1}开始
{:好的,在}=Date.from{2017,1,5}结束
查询=来自发票中的i,其中:i.发出日期>=^开始日期,
其中:i.发布于使用外部查询加入:
在您的情况下,它将是:
query = from i in Invoice, where: i.issued_on >= ^starts_on,
where: i.issued_on <= ^ends_on,
join: p in assoc(i, :products),
where: p ...,
preload: [:products]
query=from发票中的i,其中:i.issued\u on>=^start\u on,
其中:i.发布于使用外部查询加入:
在您的情况下,它将是:
query = from i in Invoice, where: i.issued_on >= ^starts_on,
where: i.issued_on <= ^ends_on,
join: p in assoc(i, :products),
where: p ...,
preload: [:products]
query=from发票中的i,其中:i.issued\u on>=^start\u on,
地点:i.U.你测试过吗?我有一种感觉,它比这更复杂,可能需要一个嵌套查询。像这样一个简单的连接将复制符合where
条件的每个产品p
的发票,例如,如果它是p.category==“蔬菜”
,并且一张发票有多个蔬菜产品,返回的列表将包含该发票的多个副本。可能需要一个group_by或者其他什么。或者不同的查询。你测试过吗?我有一种感觉,它比这更复杂,可能需要一个嵌套查询。像这样一个简单的连接将复制符合where
条件的每个产品p
的发票,例如,如果它是p.category==“蔬菜”
,并且一张发票有多个蔬菜产品,返回的列表将包含该发票的多个副本。可能需要一个group_by或其他什么。或不同的查询。