Category: Supabase and databases

Supabase upsert does not work: ON CONFLICT and no UNIQUE

Read this guide in Polish

Integration was supposed to update existing contact, but creates duplicate or returns conflict error.

Why is this happening?

Upsert needs a clear conflict key consistent with the limitation of PRIMARY KEY or UNIQUE. The e-mail field alone does not guarantee proper behavior without an appropriate index.

Step by step solution

1. Set a stable event identifier or an external record, not just a name or phone.

2 . Add an appropriate unique index in the database, after having safely ordered existing duplicates.

3 . In the upsert query, indicate onConflict according to the index.

4 . Check for simultaneous delivery of two identical webhooks and behavior with network error.

5 . Separate the deduplication of events from combining several authentic reports from the same person.

How do I check the score?

A double processing of the same event shall not create a double entry or subsequent notification.

Uniqueness after the phone number itself can glue different people or two separate applications. Design the key according to the event definition.