Querying composite fields with ecto / accessing AS prefix

Is there a way to query a composite field in ecto? I’m using the :money library and trying to do some queries on the inner amount field.1

In raw SQL this would be something like

SELECT cost FROM composite_currency WHERE (cost).amount > 100;

Of course I can use a fragment:

import Ecto.Query
from c in CompositeCurrency, where: fragment("(cost).amount") > 100

But my concern is SQL it returns which is

SELECT c0."id", c0."cost", c0."inserted_at", c0."updated_at" FROM "composite_currency" AS c0 WHERE ((cost).amount > 100) []

One thing I fear is I will need to do some query that will require that prefix c0 and I’m not sure how to access it.

Does anyone know if there’s a way to access composite fields with the query DSL or know of a way to access the AS prefix?

1 The money library has an Ecto Type called Money.Ecto.Composite.Type that uses a composite type in Postgres to allow for a money field that also has its currency.

fragment("(?).amount", c.cost)
# or maybe
fragment("(?).?", c.cost, "amount")
1 Like

Aaaah thank you that worked!