postgrest-js icon indicating copy to clipboard operation
postgrest-js copied to clipboard

Allow for custom indexes in `onConflict` upsert parameter

Open kylerummens opened this issue 2 years ago • 1 comments

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('*');

kylerummens avatar Jan 03 '23 03:01 kylerummens

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.

steve-chavez avatar Jan 04 '23 04:01 steve-chavez