elixir - Ecto declare schema for a query -
i'm trying run query:
select last_sd.* (select distinct(sensor_id) sensor_data) s left join lateral (select * sensor_data sd1 sd1.sensor_id = s.sensor_id order sd1.received_at desc limit 1) last_sd on true
the closest got is:
from s in iotinabox.sensordata, distinct: true, select: s.sensor_id |> join(:left_lateral, [s], sd in fragment("select * sensor_data sd1 sd1.sensor_id = ? order sd1.received_at desc limit 1", s.sensor_id)) |> select([s, sd], sd)
this works partially, since throws
postgresql requires schema module when using selector "f1" none given. please specify schema or specify fields "f1" desire in query
meaning since don't have from s in sensordata
doesn't know ecto model use,
is there way tell ecto schema use query result?
maybe not perfect works me (using names project in example below):
keys = division.__schema__(:fields) query = d in "divisions", select: map(d, ^keys) result = repo.all(query) |> enum.map(&(struct(division, &1)))
- first need list of schema fields
- select keys in form of map
- map maps structs ^^
Comments
Post a Comment