@@ -124,11 +124,11 @@ let
124124 (OnConflictDoUpdate
125125 (UniqueIndexOn #col1)
126126 (Set 2 `as` #col1 :* Same `as` #col2)
127− [#col1 .== #col2])
127+ [#existing ! #col1 .== #excluded ! #col2])
128128 (Returning $ (#col1 + #col2) `as` #sum)
129129in printSQL manipulation
130130:}
131−INSERT INTO "tab" ("col1", "col2") VALUES (2, 4), (6, 8) ON CONFLICT ("col1") DO UPDATE SET "col1" = 2 WHERE ("col1" = "col2") RETURNING ("col1" + "col2") AS "sum"
131+INSERT INTO "tab" AS existing ("col1", "col2") VALUES (2, 4), (6, 8) ON CONFLICT ("col1") DO UPDATE SET "col1" = 2 WHERE ("existing"."col1" = "excluded"."col2") RETURNING ("col1" + "col2") AS "sum"
132132
133133query insert:
134134
@@ -245,6 +245,7 @@ insertRows
245245 -> Manipulation schema params results
246246insertRows tab rw rws conflict returning = UnsafeManipulation $
247247 "INSERT" <+> "INTO" <+> renderAlias tab
248+ <> renderConflictAlias conflict
248249 <+> parenthesized (renderCommaSeparated renderAliasPart rw)
249250 <+> "VALUES"
250251 <+> commaSeparated
@@ -312,6 +313,7 @@ insertQuery
312313 -> Manipulation schema params results
313314insertQuery tab query conflict returning = UnsafeManipulation $
314315 "INSERT" <+> "INTO" <+> renderAlias tab
316+ <> renderConflictAlias conflict
315317 <+> renderQuery query
316318 <> renderConflictClause conflict
317319 <> renderReturningClause returning
@@ -334,15 +336,15 @@ insertQuery_ tab query =
334336-- existing value in the row for an update.
335337data ColumnValue
336338 (schema :: SchemaType)
337− (columns :: RowType)
339+ (tables :: FromType)
338340 (params :: [NullityType])
339341 (ty :: ColumnType)
340342 where
341− Same :: ColumnValue schema (column ': columns) params ty
342− Default :: ColumnValue schema columns params ('Def :=> ty)
343+ Same :: ColumnValue schema (table ': tables) params ty
344+ Default :: ColumnValue schema tables params ('Def :=> ty)
343345 Set
344− :: (forall table. Expression schema '[table ::: columns] 'Ungrouped params ty)
345− -> ColumnValue schema columns params (constraint :=> ty)
346+ :: Expression schema tables 'Ungrouped params ty
347+ -> ColumnValue schema tables params (constraint :=> ty)
346348
347349-- | A `ReturningClause` computes and return value(s) based
348350-- on each row actually inserted, updated or deleted. This is primarily
@@ -391,12 +393,12 @@ data ConflictClause
391393 OnConflictDoUpdate
392394 :: (row ~ TableToRow table, columns ~ TableToColumns table)
393395 => ConflictTarget table
394− -> NP (Aliased (ColumnValue schema row params)) columns
395− -> [Condition schema '[t ::: row] 'Ungrouped params]
396+ -> NP (Aliased (ColumnValue schema '["existing" ::: row, "excluded" ::: row] params)) columns
397+ -> [Condition schema '["existing" ::: row, "excluded" ::: row] 'Ungrouped params]
396398 -> ConflictClause schema table params
397399
398−-- | A 'ConflictTarget' specifies the constraint violation that triggers an
399−-- 'OnConflictDoUpdate'.
400+-- | A `ConflictTarget` specifies the constraint violation that triggers an
401+-- `OnConflictDoUpdate`.
400402data ConflictTarget (table :: TableType) where
401403 -- | An explicitly named constraint.
402404 OnConstraint
@@ -410,6 +412,12 @@ data ConflictTarget (table :: TableType) where
410412 => NP Alias columnNames
411413 -> ConflictTarget '(constraints, columns)
412414
415+-- | Render any table alias required for a `ConflictClause`.
416+renderConflictAlias :: ConflictClause schema table params -> ByteString
417+renderConflictAlias = \case
418+ OnConflictDoUpdate _ _ _ -> " AS existing"
419+ _ -> ""
420+
413421-- | Render a `ConflictClause`.
414422renderConflictClause
415423 :: SOP.SListI (TableToColumns table)
@@ -458,7 +466,7 @@ update
458466 , row ~ TableToRow table
459467 , columns ~ TableToColumns table )
460468 => Alias tab -- ^ table to update
461− -> NP (Aliased (ColumnValue schema row params)) columns
469+ -> (forall t. NP (Aliased (ColumnValue schema '[t ::: row] params)) columns)
462470 -- ^ modified values to replace old values
463471 -> (forall t. Condition schema '[t ::: row] 'Ungrouped params)
464472 -- ^ condition under which to perform update on a row
@@ -489,7 +497,7 @@ update_
489497 , row ~ TableToRow table
490498 , columns ~ TableToColumns table )
491499 => Alias tab -- ^ table to update
492− -> NP (Aliased (ColumnValue schema row params)) columns
500+ -> (forall t. NP (Aliased (ColumnValue schema '[t ::: row] params)) columns)
493501 -- ^ modified values to replace old values
494502 -> (forall t. Condition schema '[t ::: row] 'Ungrouped params)
495503 -- ^ condition under which to perform update on a row