Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add on_conflict in upsert function #201

Closed
tw1t611 opened this issue Jan 27, 2023 · 2 comments
Closed

Add on_conflict in upsert function #201

tw1t611 opened this issue Jan 27, 2023 · 2 comments

Comments

@tw1t611
Copy link

tw1t611 commented Jan 27, 2023

In the current version upsert() does not support on_conflict.
I guess #139 is required for this request to be implemented.

So here is a workaround for now, if someone comes across this issue:

def upsert(table_name, data, on_conflict, select):
    url = f'https://PROJECT_ID.supabase.co/rest/v1/{table_name}?on_conflict={on_conflict}&select={select}'
    headers = {"Content-Type": "application/json",
               "apiKey": "SECRET",
               "authorization": f'Bearer SECRET',
               "Prefer": "resolution=merge-duplicates,return=representation"}
    response = requests.post(url, headers=headers, json=data)
    return response.status_code

# example
response = upsert('automations', [{"name":"Germany"},{"name":"Italy"}], 'name', 'id')

The final solution could look like this:

data = supabase.table("countries").upsert([{"name":"Germany"}], on_conflict=["name"]).execute()
@J0
Copy link
Contributor

J0 commented Jan 29, 2023

Thanks for the workaround!

Going to move this to the postgrest-py repo so we can track. Hopefully we can push on_conflcit option over the finish line next weekend

@J0 J0 transferred this issue from supabase-community/supabase-py Jan 29, 2023
@J0
Copy link
Contributor

J0 commented Feb 7, 2023

Fixed by #142 hopefully - going to close but feel free to let me know if it doesn't work for you

@J0 J0 closed this as completed Feb 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants