I’m having trouble with a simple join/preload query that is returning incorrectly.
I have three tables: Books, Editions, and RegistryBooks. RegistryBooks has two rows
%{book_id: 1, edition_id: 1}
%{book_id: 1, edition_id: 2}
I want to build a query that fetches both rows and preloads both the book and the edition.
Market.RegistryBook
|> join(:inner, [rb], b in Library.Book, on: b.id == rb.book_id)
|> join(:inner, [rb, b], e in Library.BookEdition, on: e.id == rb.edition_id)
|> preload([rb, b, e], book: {b, edition: e})
|> Repo.all()
Two rows are being returned, but the issue is that even though the ReaderBook has the correct edition_id, the nested edition is wrong. It always matches whatever is the first result. So in this case both preloaded nested editions are id: 1.
%{book_id: 1, edition_id: 1, book: %{edition: %{id: 1}}}
%{book_id: 1, edition_id: 2, book: %{edition: %{id: 1}}}
I’ve run the same query on the database directly and it returns the correctly joined edition, so maybe this is an issue with Ecto? What am I missing here?