Search code examples
elixirecto

Using :inner_lateral in Ecto Elixir


I was trying to use ecto's :inner_lateral And I wrote this query

SnapshotExtractor
|> order_by(desc: :created_at)
|> limit(1)
|> join(:inner_lateral, [se], cam in fragment("SELECT * FROM cameras as cam WHERE cam.id = ?", se.camera_id))
|> select([se, cam], { se.from_date, se.to_date, se.interval, se.schedule, cam.exid, cam.timezone})
|> Repo.one

Its working fine and giving me good resuls but its giving me an un-named object, no name with the resulting values.. Question is can we add names to the values as well? my resulting values are

{#Ecto.DateTime<2016-02-25 00:00:00>, #Ecto.DateTime<2016-02-25 00:00:00>, 0,
 %{"Friday" => [], "Monday" => ["0:0-0:0"], "Saturday" => [], "Sunday" => [],
   "Thursday" => [], "Tuesday" => [], "Wednesday" => []}, "oscar2", nil}

Solution

  • The thing you're referring to as "unnamed object" is a tuple. You're getting that back because your select expression is a tuple. You probably want a Map if you want to give them names. To get back a Map, just return a Map in the select expression:

    |> select([se, cam], %{ from_date: se.from_date, to_date: se.to_date, interval: se.interval, schedule: se.schedule, exid: cam.exid, timezone: cam.timezone})
    

    If you assign the return value to foo, you can access the fields using foo.from_date, foo.to_date, etc.