@@ -26,6 +26,7 @@ def __init__(self, model=None, query=None, using=None, hints=None):
26
26
27
27
self .conflict_target = None
28
28
self .conflict_action = None
29
+ self .index_predicate = None
29
30
30
31
def annotate (self , ** annotations ):
31
32
"""Custom version of the standard annotate function
@@ -113,7 +114,7 @@ def update(self, **fields):
113
114
# affected, let's do the same
114
115
return len (rows )
115
116
116
- def on_conflict (self , fields : List [Union [str , Tuple [str ]]], action ):
117
+ def on_conflict (self , fields : List [Union [str , Tuple [str ]]], action , index_predicate : str = None ):
117
118
"""Sets the action to take when conflicts arise when attempting
118
119
to insert/create a new row.
119
120
@@ -123,10 +124,16 @@ def on_conflict(self, fields: List[Union[str, Tuple[str]]], action):
123
124
124
125
action:
125
126
The action to take when the conflict occurs.
127
+
128
+ index_predicate:
129
+ The index predicate to satisfy an arbiter partial index (i.e. what partial index to use for checking
130
+ conflicts)
126
131
"""
127
132
128
133
self .conflict_target = fields
129
134
self .conflict_action = action
135
+ self .index_predicate = index_predicate
136
+
130
137
return self
131
138
132
139
def bulk_insert (self , rows ):
@@ -216,7 +223,7 @@ def insert_and_get(self, **fields):
216
223
217
224
return self .model (** model_init_fields )
218
225
219
- def upsert (self , conflict_target : List , fields : Dict ) -> int :
226
+ def upsert (self , conflict_target : List , fields : Dict , index_predicate : str = None ) -> int :
220
227
"""Creates a new record or updates the existing one
221
228
with the specified data.
222
229
@@ -227,11 +234,15 @@ def upsert(self, conflict_target: List, fields: Dict) -> int:
227
234
fields:
228
235
Fields to insert/update.
229
236
237
+ index_predicate:
238
+ The index predicate to satisfy an arbiter partial index (i.e. what partial index to use for checking
239
+ conflicts)
240
+
230
241
Returns:
231
242
The primary key of the row that was created/updated.
232
243
"""
233
244
234
- self .on_conflict (conflict_target , ConflictAction .UPDATE )
245
+ self .on_conflict (conflict_target , ConflictAction .UPDATE , index_predicate )
235
246
return self .insert (** fields )
236
247
237
248
def upsert_and_get (self , conflict_target : List , fields : Dict ):
@@ -307,6 +318,7 @@ def _build_insert_compiler(self, rows: List[Dict]):
307
318
query = PostgresInsertQuery (self .model )
308
319
query .conflict_action = self .conflict_action
309
320
query .conflict_target = self .conflict_target
321
+ query .index_predicate = self .index_predicate
310
322
query .values (objs , insert_fields , update_fields )
311
323
312
324
# use the postgresql insert query compiler to transform the insert
@@ -466,7 +478,7 @@ def on_conflict(self, fields: List[Union[str, Tuple[str]]], action):
466
478
"""
467
479
return self .get_queryset ().on_conflict (fields , action )
468
480
469
- def upsert (self , conflict_target : List , fields : Dict ) -> int :
481
+ def upsert (self , conflict_target : List , fields : Dict , index_predicate : str = None ) -> int :
470
482
"""Creates a new record or updates the existing one
471
483
with the specified data.
472
484
@@ -477,11 +489,14 @@ def upsert(self, conflict_target: List, fields: Dict) -> int:
477
489
fields:
478
490
Fields to insert/update.
479
491
492
+ index_predicate:
493
+ The index predicate to satisfy an arbiter partial index.
494
+
480
495
Returns:
481
496
The primary key of the row that was created/updated.
482
497
"""
483
498
484
- return self .get_queryset ().upsert (conflict_target , fields )
499
+ return self .get_queryset ().upsert (conflict_target , fields , index_predicate )
485
500
486
501
def upsert_and_get (self , conflict_target : List , fields : Dict ):
487
502
"""Creates a new record or updates the existing one
0 commit comments