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.
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?
rb.id is what I want. Thanks for the tip on the Ecto SQL adapter. I’ll look at the generated SQL to see how it differs from what I wrote. One thing that is working is if I replace the preload with a select
Ah yes, that does make sense. I want RegistryBook structs with the same Book struct except, that the edition field of the Book struct should have a different BookEdition struct.
BookEdition has a belongs_to relationship with Book. Book has a has_many relationship with BookEdition.