[Django] #34225: django 4.1.x - bulk_create - django.db.utils.IntegrityError: duplicate key value violates unique constraint

102 views
Skip to first unread message

Django

unread,
Dec 22, 2022, 1:18:44 PM12/22/22
to django-...@googlegroups.com
#34225: django 4.1.x - bulk_create - django.db.utils.IntegrityError: duplicate key
value violates unique constraint
-------------------------------------+-------------------------------------
Reporter: Victor J. | Owner: nobody
Fuente |
Type: Bug | Status: new
Component: Database | Version: 4.1
layer (models, ORM) |
Severity: Normal | Keywords: #bulk_create
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
bulk_create on 4.1 perform a bulk_update when a unique constraint fails.

But on this example, when the constraint fails, an integrity Error occur
and no update is in place


{{{

class Test(models.Model):

field1 = models.IntegerField(null=True, blank=True)
field2 = models.IntegerField(null=True, blank=True)
count = models.IntegerField(null=True, blank=True)

class Meta:

db_table = "test"

constraints = [
models.UniqueConstraint(
models.functions.Coalesce("field1", 0),
models.functions.Coalesce("field2", 0),
name="test_coal_unique",
),
models.UniqueConstraint(
"field1",
"field2",
name="test_unique",
),
]


t1 = Test(field1=1,field2=None,count=1)
t2 = Test(field1=1,field2=None,count=2)

Test.objects.bulk_create([t1, t2], ignore_conflicts=False,
update_conflicts=True, unique_fields=['field1','field2'], update_fields =
['count'])

}}}

{{{
django.db.utils.IntegrityError: duplicate key value violates unique
constraint "test_coal_unique"
DETAIL: Key (COALESCE(field1, 0), COALESCE(field2, 0))=(1, 0) already
exists.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34225>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Reply all
Reply to author
Forward
0 new messages