Reputation: 8318
How can I preload a has many through association?
mix phx.new shop
cd shop
mix echo.create
mix phx.gen.html Accounting Invoice invoices number issued_on:date
mix phx.gen.html Accounting Product products name price:integer
mix phx.gen.html Accounting LineItem line_items
invoice_id:references:invoices
product_id:references:products
mix ecto.migrate
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: "2017-01-01"})
{:ok, invoice3} = Shop.Accounting.create_invoice(%{number: "3", issued_on: "2017-01-02"})
{:ok, banana} = Shop.Accounting.create_product(%{name: "Banana", price: 1})
{:ok, apple} = Shop.Accounting.create_product(%{name: "Apple", price: 2})
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: apple.id})
Shop.Accounting.create_line_item(%{invoice_id: invoice3.id, product_id: apple.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, :products]
timestamps()
end
lib/shop/accounting/line_item.ex
schema "line_items" do
belongs_to :invoice, Shop.Accounting.Invoice
belongs_to :product, Shop.Accounting.Product
timestamps()
end
lib/shop/accounting/product.ex
schema "products" do
field :name, :string
field :price, :integer
timestamps()
end
With the following code I try to query all invoices
from the 1st of January to the 5th of January including the products
. Without the preload
it works. What do I have to change to preload the products
?
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)
I get this error:
iex(15)> invoices = Repo.all(query)
[debug] QUERY OK source="invoices" db=2.6ms decode=0.1ms
SELECT i0."id", i0."issues_on", i0."number", i0."inserted_at", i0."updated_at" FROM "invoices" AS i0 WHERE (i0."issues_on" >= $1) AND (i0."issues_on" <= $2) [{2017, 1, 1}, {2017, 1, 5}]
[debug] QUERY OK source="line_items" db=2.8ms
SELECT l0."id", l0."invoice_id", l0."product_id", l0."inserted_at", l0."updated_at", l0."invoice_id" FROM "line_items" AS l0 WHERE (l0."invoice_id" = ANY($1)) ORDER BY l0."invoice_id" [[3, 2, 1]]
** (UndefinedFunctionError) function nil.__struct__/0 is undefined or private
nil.__struct__()
(elixir) lib/enum.ex:1811: Enum."-reduce/3-lists^foldl/2-0-"/3
(elixir) lib/enum.ex:1811: Enum."-reduce/3-lists^foldl/2-0-"/3
(elixir) lib/enum.ex:1811: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto) lib/ecto/repo/queryable.ex:139: Ecto.Repo.Queryable.execute/5
(ecto) lib/ecto/repo/queryable.ex:37: Ecto.Repo.Queryable.all/4
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(stdlib) erl_eval.erl:438: :erl_eval.expr/5
(iex) lib/iex/evaluator.ex:219: IEx.Evaluator.handle_eval/5
(iex) lib/iex/evaluator.ex:200: IEx.Evaluator.do_eval/3
(iex) lib/iex/evaluator.ex:178: IEx.Evaluator.eval/3
(iex) lib/iex/evaluator.ex:77: IEx.Evaluator.loop/1
(iex) lib/iex/evaluator.ex:21: IEx.Evaluator.init/4
Upvotes: 1
Views: 666
Reputation: 9831
Here is what is wrong in your invoices:
has_many :products, through: [:line_items, :products]
Products are plural. You should have singular product
instead:
has_many :products, through: [:line_items, :product]
This is because in line items you have singular product:
belongs_to :product, Shop.Accounting.Product
And has_many through
follows already described associations.
Upvotes: 2