Specifying Ecto Bindings at runtime


We have a use case where users should be able to define their own models which are stored at runtime in a generated database. To query these models we want to generate queries using Ecto, but we stumbled on a problem that bindings are evaluated on compile time.

Therefore the following, won’t compile:

root_table = "root"
root_name = :root

assoc_table = "associated"
assoc_name = :assoc_1

query = from(r in root_table, as: ^root_name)
|> join(:left, [{root_name, r}], a in ^assoc_table, on: field(r, :id) == field(a, :assoc_id), as: ^assoc_name)

However as our schema is not yet known at compile time we cannot provide these bindings. Are we reaching the limit of what you are able to do with ecto, or are there other routes we could explore?

There’s no [{root_name, r}] for bindings. There’s just [{variable, position}] for getting a binding by position: https://github.com/elixir-ecto/ecto/issues/2832

Try looking into schemaless queries: http://blog.plataformatec.com.br/2016/05/ectos-insert_all-and-schemaless-queries/

1 Like