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))) 
  1. first need list of schema fields
  2. select keys in form of map
  3. map maps structs ^^

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -