supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
967 stars 130 forks source link

Allow for custom indexes in `onConflict` upsert parameter #382

Open kylerummens opened 1 year ago

kylerummens commented 1 year ago

Feature request

Is your feature request related to a problem? Please describe.

The upsert method has an optional onConflict which is described in the docs as:

Comma-separated UNIQUE column(s) to specify how duplicate rows are determined. Two rows are duplicates if all the onConflict columns are equal.

I'm trying to do the following:

const { data, error } = await this.supabase.client
    .from('my_table')
    .upsert(my_data, { onConflict: 'my_custom_unique_index', ignoreDuplicates: true })
    .select('*');

and I am getting the following error:

column "my_custom_unique_index" does not exist

Looking under the hood, it looks like the PostgreSQL is being generated like this:

ON CONFLICT("my_custom_unique_index") DO NOTHING

The above sql would work without the double quotes around the index name.

Describe the solution you'd like

A way to specify a unique index to be used in the ON CONFLICT statement, instead of a column name.

I don't know what the js would look like, but an example could be:

const { data, error } = await this.supabase.client
    .from('my_table')
    .upsert(my_data, { onConflict: 'my_custom_unique_index', onConflictType: 'index', ignoreDuplicates: true })
    .select('*');
steve-chavez commented 1 year ago

The above sql would work without the double quotes around the index name.

Can you show an example of that working in plain SQL?

According to postgresql docs, on conflict only works on column names or constraint names.