Skip to content

Inserting default values when using subqueries #359

Answered by echatav
magthe asked this question in Q&A
Discussion options

You must be logged in to vote

Instead of Subquery use Select. That will let you use Default in your subquery.

https://hackage.haskell.org/package/squeal-postgresql-0.9.1.3/docs/Squeal-PostgreSQL-Manipulation-Insert.html#t:QueryClause

Here's an example:

>>> :{
let
  insertUser :: Statement DB User ()
  insertUser = manipulation $ with (u `as` #u) e
    where
      u = insertInto #users
        (Values_ (Default `as` #id :* Set (param @1) `as` #name))
        OnConflictDoRaise (Returning_ (#id :* param @2 `as` #email))
      e = insertInto_ #emails $ Select
        (Default `as` #id :* Set (#u ! #id) `as` #user_id :* Set (#u ! #email) `as` #email)
        (from (common #u))
:}

Replies: 2 comments 10 replies

Comment options

You must be logged in to vote
8 replies
@echatav
Comment options

@echatav
Comment options

@echatav
Comment options

@magthe
Comment options

@echatav
Comment options

Answer selected by magthe
Comment options

You must be logged in to vote
2 replies
@echatav
Comment options

@echatav
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants