Complex joins with Ecto.Query.dynamic/2

I’m using Ecto.Query.dynamic/2 to conditionally build queries piece-meal.

Following this: https://hexdocs.pm/ecto/dynamic-queries.html#dynamic-and-joins

The examples work, but they are a bit basic. I have some tested joins. For example:

dynamic([authors: a], ^dynamic and a.name == ^value)

I actually also need to load each author’s :books as b, and check one of b’s columns against a list of values.

I tried:

 dynamic([authors: {a, books: b}], ^dynamic and b.publisher_id in ^ids)

But this gives:

(Ecto.Query.CompileError) binding list should contain only variables or `{as, var}` tuples, got: {:author, {a, [books: b]}}

Dynmic uses the same syntax as all the ecto query function based api: [author: a, books: b] Dynamic doesn‘t care how bindings come into existence for as long as they do exist once attached to a query.

1 Like