Conflicts (unless conflict) — AQL
Handling conflicts (unless conflict)
Section titled “Handling conflicts (unless conflict)”An insert may declare what to do when it collides with an existing row on a
unique (exclusive) or primary-key constraint. This lowers to Postgres
ON CONFLICT.
Do nothing on any conflict:
insert User { email := $email, name := $name } unless conflict;INSERT INTO "user" ("email", "name")VALUES ($1, $2)ON CONFLICT DO NOTHINGRETURNING *;Do nothing on a specific constraint:
insert User { email := $email, name := $name } unless conflict on .email;INSERT INTO "user" ("email", "name")VALUES ($1, $2)ON CONFLICT ("email") DO NOTHINGRETURNING *;Use on (.a, .b) to target a composite exclusive constraint.
Upsert — update the existing row on conflict (else):
insert User { email := $email, name := $name }unless conflict on .emailelse (update User set { name := $name });INSERT INTO "user" ("email", "name")VALUES ($1, $2)ON CONFLICT ("email") DO UPDATE SET "name" = $2RETURNING *;Rules and behavior:
- The
ontarget must be backed by anexclusiveor primary-key constraint; otherwise compilation fails. elserequires anontarget, its type must match the insert’s type, and it takes nofilter(Postgres targets the conflicting row automatically).RETURNINGbehavior differs by form:DO UPDATEreturns the updated row, butDO NOTHINGreturns no row when a conflict occurs. Handle the empty result in calling code for theunless conflict/unless conflict on ...forms.- The clause is supported on top-level inserts only (not nested
(insert ...)link sub-inserts).