dylex / postgresql-typed

Haskell PostgreSQL library with compile-time type inference
http://hackage.haskell.org/package/postgresql-typed
Other
83 stars 12 forks source link

How to handle CTE queries? #31

Closed mistyharsh closed 1 year ago

mistyharsh commented 1 year ago

Thanks for the amazing library. Probably a newbie question, but I would try my luck. How do I handle queries with common table expressions? For example:

createNewImage =
  [pgSQL|
    WITH asset_cet AS (
      INSERT INTO
        asset (
          storage_id,
          title,
          file_name,
          content_type,
          size,
          size_unit,
          created_at,
          updated_at,
          verified,
          publication_id
        )
      VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
      RETURNING id AS asset_id
    )
    INSERT INTO
      image (
        id,
        caption,
        alt_text
      )
      SELECT asset_id, $11, $12 FROM asset_cte
    RETURNING caption;
  |]

I get the error that asset_cet does not exist:

• Exception when trying to run compile-time code:
     PGERROR [42P01]: relation "asset_cte" does not exist

I could not find any closed issues or examples with CTE queries.

Sidenote: I am using postgresql v15.1 with ghc v9.4.4

dylex commented 1 year ago

They should work fine as is. I think you have a typo in your example: asset_cet vs asset_cte.

mistyharsh commented 1 year ago

Holy FCK... I spent hours trying to find the problem. My apology for wasting your time on a trivial thing. Thank you! I would now close this issue.