Skip to content

Commit ecc6e79

Browse files
Oisanguianand2312
andauthored
feat: upsert with on-conflict support (#142)
* feat: upsert with on-conflict support * fix: lint * Update postgrest/base_request_builder.py Co-authored-by: Anand <40204976+anand2312@users.noreply.github.com> * chore: docs * chore: docs --------- Co-authored-by: Anand <40204976+anand2312@users.noreply.github.com>
1 parent 0fa4e4e commit ecc6e79

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

postgrest/_async/request_builder.py

+3
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ def upsert(
262262
count: Optional[CountMethod] = None,
263263
returning: ReturnMethod = ReturnMethod.representation,
264264
ignore_duplicates: bool = False,
265+
on_conflict: str = "",
265266
) -> AsyncQueryRequestBuilder:
266267
"""Run an upsert (INSERT ... ON CONFLICT DO UPDATE) query.
267268
@@ -270,6 +271,7 @@ def upsert(
270271
count: The method to use to get the count of rows returned.
271272
returning: Either 'minimal' or 'representation'
272273
ignore_duplicates: Whether duplicate rows should be ignored.
274+
on_conflict: Specified columns to be made to work with UNIQUE constraint.
273275
Returns:
274276
:class:`AsyncQueryRequestBuilder`
275277
"""
@@ -278,6 +280,7 @@ def upsert(
278280
count=count,
279281
returning=returning,
280282
ignore_duplicates=ignore_duplicates,
283+
on_conflict=on_conflict
281284
)
282285
return AsyncQueryRequestBuilder(
283286
self.session, self.path, method, headers, params, json

postgrest/_sync/request_builder.py

+3
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ def upsert(
262262
count: Optional[CountMethod] = None,
263263
returning: ReturnMethod = ReturnMethod.representation,
264264
ignore_duplicates: bool = False,
265+
on_conflict: str = "",
265266
) -> SyncQueryRequestBuilder:
266267
"""Run an upsert (INSERT ... ON CONFLICT DO UPDATE) query.
267268
@@ -270,6 +271,7 @@ def upsert(
270271
count: The method to use to get the count of rows returned.
271272
returning: Either 'minimal' or 'representation'
272273
ignore_duplicates: Whether duplicate rows should be ignored.
274+
on_conflict: Specified columns to be made to work with UNIQUE constraint.
273275
Returns:
274276
:class:`SyncQueryRequestBuilder`
275277
"""
@@ -278,6 +280,7 @@ def upsert(
278280
count=count,
279281
returning=returning,
280282
ignore_duplicates=ignore_duplicates,
283+
on_conflict=on_conflict
281284
)
282285
return SyncQueryRequestBuilder(
283286
self.session, self.path, method, headers, params, json

postgrest/base_request_builder.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,18 @@ def pre_upsert(
7070
count: Optional[CountMethod],
7171
returning: ReturnMethod,
7272
ignore_duplicates: bool,
73+
on_conflict: str= "",
7374
) -> QueryArgs:
75+
query_params = dict()
7476
prefer_headers = [f"return={returning}"]
7577
if count:
7678
prefer_headers.append(f"count={count}")
7779
resolution = "ignore" if ignore_duplicates else "merge"
7880
prefer_headers.append(f"resolution={resolution}-duplicates")
7981
headers = Headers({"Prefer": ",".join(prefer_headers)})
80-
return QueryArgs(RequestMethod.POST, QueryParams(), headers, json)
82+
if on_conflict:
83+
query_params["on_conflict"] = on_conflict
84+
return QueryArgs(RequestMethod.POST, QueryParams(query_params), headers, json)
8185

8286

8387
def pre_update(

0 commit comments

Comments
 (0)