#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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/010701853b0f13f0-68a189bf-2693-43bf-ad7f-a12e65e3e42d-000000%40eu-central-1.amazonses.com.