When upserting to my table, i have two constrains that I both want to add an onConflict for. I will be upserting data that conflicts with either of the two, but never both.
The constrains on the table are:
constraint patients_pkey primary key (id),
constraint patients_id_key unique (id),
constraint patients_source_foreign_id_unique unique (source, foreign_id),
So I tried:
const { data, error } = await supabase
.from('patients')
.upsert(
{
id: patient.id ?? undefined,
belongs_to_user: user.id,
belongs_to_clinic: clinic.id,
name: patient.name,
identification_number: patient.identification_number,
notes: patient.notes,
source: patient.source,
foreign_id: patient.foreign_id
},
{
onConflict: 'id, (source, foreign_id)' // herein lies the problem,
}
)
.select()
.single();
The above code of course throws an error:
{
"code": "PGRST100",
"details": "unexpected "(" expecting field name (* or [a..z0..9_$])",
"hint": null,
"message": ""failed to parse on_conflict parameter (id, (source, foreign_id))" (line 1, column 5)"
}
Since Supabase adds brackets, so i tried to escape them:
onConflict: 'id), (source, foreign_id'
Which escapes correctly, but to no avail again:
duplicate key value violates unique constraint "patients_source_foreign_id_unique"
I did not find mention of this in the documentation, I am trying to satisfy two constrains, not two columns in the same constraint!