Ecto fragment as a query source?

I ran into this recently. The work around I found was to use a CTE:

"time_slots"
|> with_cte("time_slots",
  as:
    fragment("""
    SELECT generate_series(
      '2020-07-13 10:00:00'::timestamp,
      '2020-07-13 11:00:00'::timestamp,
      '15 minutes') as time_slot
    """)
)
|> from(as: :time_slots)
|> select([time_slots: t], t.time_slot)
|> except(^(Availability |> select([a], a.time_slot)))

Which should produce the SQL:

WITH "time_slots" AS (
  SELECT generate_series(
    '2020-07-13 10:00:00'::timestamp, 
    '2020-07-13 11:00:00'::timestamp, 
    '15 minutes') as time_slot
) 
SELECT t0."time_slot" 
FROM "time_slots" AS t0 
EXCEPT (
  SELECT a0."time_slot" 
  FROM "availabilities" AS a0
)
5 Likes