File tree Expand file tree Collapse file tree 2 files changed +46
-1
lines changed
Expand file tree Collapse file tree 2 files changed +46
-1
lines changed Original file line number Diff line number Diff line change @@ -253,9 +253,12 @@ def _format_field_value(self, field_name) -> str:
253253 in SQL.
254254 """
255255
256+ if isinstance (field_name , tuple ):
257+ field_name , _ = field_name
258+
256259 field = self ._get_model_field (field_name )
257260 return SQLInsertCompiler .prepare_value (
258261 self ,
259262 field ,
260- getattr (self .query .objs [0 ], field . name )
263+ getattr (self .query .objs [0 ], field_name )
261264 )
Original file line number Diff line number Diff line change @@ -263,3 +263,45 @@ def test_on_conflict_unique_together(conflict_action):
263263 )
264264
265265 assert id1 == id2
266+
267+
268+ @pytest .mark .parametrize ("conflict_action" , CONFLICT_ACTIONS )
269+ def test_on_conflict_unique_together2 (conflict_action ):
270+ """Asserts that inserts on models with a unique_together
271+ works properly."""
272+
273+ model = get_fake_model (
274+ {
275+ 'name' : models .CharField (max_length = 140 ),
276+ },
277+ )
278+
279+ model2 = get_fake_model (
280+ {
281+ 'model1' : models .ForeignKey (model ),
282+ 'model2' : models .ForeignKey (model )
283+ },
284+ PostgresModel ,
285+ {
286+ 'unique_together' : ('model1' , 'model2' )
287+ }
288+ )
289+
290+ id1 = model .objects .create (name = 'one' ).id
291+ id2 = model .objects .create (name = 'two' ).id
292+
293+ assert id1 != id2
294+
295+ id3 = (
296+ model2 .objects
297+ .on_conflict (['model1_id' , 'model2_id' ], conflict_action )
298+ .insert (model1_id = id1 , model2_id = id2 )
299+ )
300+
301+ id4 = (
302+ model2 .objects
303+ .on_conflict (['model1_id' , 'model2_id' ], conflict_action )
304+ .insert (model1_id = id1 , model2_id = id2 )
305+ )
306+
307+ assert id3 == id4
You can’t perform that action at this time.
0 commit comments