supabase-flutter icon indicating copy to clipboard operation
supabase-flutter copied to clipboard

upsert fails to pass onConflict into the on_conflict in postgREST

Open sbutlerjr opened this issue 1 year ago • 2 comments

Describe the bug When doing an UPSERT and supplying the onConflict vie the SDK, the on_conflict property and value isn't passed through to the REST API endpoint. That causes the request to fail with a 409 status code because the unique constraint is violated.

To Reproduce

Supabase.instance.client
                .from('party')
                .upsert({company_uuid: nMQXF9BL-D, uuid: PAR_642d9e4f-669e-402d-8ccd-75297f8c63a2, is_active: true, enable_sms: false, roles: [3], first_name: TEST, last_name: TEST, email: [email protected], phone: 123-876-5309, agency_uuid: 08efd21a-d25a-4c7f-a7d9-dced19e477f8, uniq: TEST:TEST}, onConflict: 'uniq');

This generates a POST request to: https://nqnopvehcfcqebspvkba.supabase.co/rest/v1/party?columns="company_uuid","uuid","is_active","enable_sms","roles","first_name","last_name","email","phone","agency_uuid","uniq"

With the correct header: Prefer "resolution=merge-duplicates" and the correct body:

[{"company_uuid":"nMQXF9BL-D","uuid":"PAR_642d9e4f-669e-402d-8ccd-75297f8c63a2","is_active":true,"enable_sms":false,"roles":[3],"first_name":"TEST","last_name":"TEST","email":"[email protected]","phone":"123-867-5309","agency_uuid":"08efd21a-d25a-4c7f-a7d9-dced19e477f8","uniq":"TEST:TEST"}]

But its missing the value I specified with onConflict: 'uniq'

Expected behavior According to PostgREST docs it should have added: on_conflict=uniq to the URL.
So the POST REST URL should have been https://nqnopvehcfcqebspvkba.supabase.co/rest/v1/party?columns="company_uuid","uuid","is_active","enable_sms","roles","first_name","last_name","email","phone","agency_uuid","uniq"&on_conflict=uniq

Version (please complete the following information): supabase_fluter: 2.10.1 Project is Postgres version:17.6.1.004, PostgREST version 13.0.5

sbutlerjr avatar Sep 22 '25 14:09 sbutlerjr

It looks like the SDK initially passes the onConflict through but then completly overwrites the URL if the data is a list in postgrest_query_builder.dart

    if (onConflict != null) {
      url = _url.replace(
        queryParameters: {
          'on_conflict': onConflict,
          ..._url.queryParameters,
        },
      );
    }

    if (values is List) {
      url = _setColumnsSearchParam(values);
    }

sbutlerjr avatar Sep 22 '25 14:09 sbutlerjr

PR created: https://github.com/supabase/supabase-flutter/pull/1228

sbutlerjr avatar Sep 22 '25 15:09 sbutlerjr