@@ -333,3 +333,57 @@ def test_on_conflict_pk_conflict_target(conflict_action):
333
333
assert obj1 .id == obj2 .id
334
334
assert obj1 .id == 0
335
335
assert obj2 .id == 0
336
+
337
+
338
+ def test_on_conflict_default_value ():
339
+ """Tests whether setting a default for a field and
340
+ not specifying it explicitely when upserting properly
341
+ causes the default value to be used."""
342
+
343
+ model = get_fake_model ({
344
+ 'title' : models .CharField (max_length = 255 , default = 'great' )
345
+ })
346
+
347
+ obj1 = (
348
+ model .objects
349
+ .on_conflict (['id' ], ConflictAction .UPDATE )
350
+ .insert_and_get (id = 0 )
351
+ )
352
+
353
+ assert obj1 .title == 'great'
354
+
355
+ obj2 = (
356
+ model .objects
357
+ .on_conflict (['id' ], ConflictAction .UPDATE )
358
+ .insert_and_get (id = 0 )
359
+ )
360
+
361
+ assert obj1 .id == obj2 .id
362
+ assert obj2 .title == 'great'
363
+
364
+
365
+ def test_on_conflict_default_value_no_overwrite ():
366
+ """Tests whether setting a default for a field, inserting
367
+ a non-default value and then trying to update it without
368
+ specifying that field doesn't result in it being overwritten."""
369
+
370
+ model = get_fake_model ({
371
+ 'title' : models .CharField (max_length = 255 , default = 'great' )
372
+ })
373
+
374
+ obj1 = (
375
+ model .objects
376
+ .on_conflict (['id' ], ConflictAction .UPDATE )
377
+ .insert_and_get (id = 0 , title = 'mytitle' )
378
+ )
379
+
380
+ assert obj1 .title == 'mytitle'
381
+
382
+ obj2 = (
383
+ model .objects
384
+ .on_conflict (['id' ], ConflictAction .UPDATE )
385
+ .insert_and_get (id = 0 )
386
+ )
387
+
388
+ assert obj1 .id == obj2 .id
389
+ assert obj2 .title == 'mytitle'
0 commit comments