#33649: bulk_create() with ignore_conflicts=True and ForeignKey fails
-------------------------------------+-------------------------------------
Reporter: Markus Friedrich | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: bulk_create | Triage Stage:
ForeignKey ignore_conflicts | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):
* status: new => closed
* cc: Hannes Ljungberg (added)
* component: Uncategorized => Database layer (models, ORM)
* resolution: => invalid
Old description:
> Creating both objects of a many-to-one relation using bulk_create
> together with ignore_conflicts=True fails since Django >=3.2!
>
> To enable many-to-one relations in bulk_create the "to_field" of the
> ForeignKey is defined via a UUIDField with a uuid.UUID4 default value.
>
> Without the ignore_conflicts=True option of bulk_create it works well in
> all Django versions.
> But if ignore_conflicts=True is needed since some of the objects using in
> the bulk_create may already be saved to the DB then it works only for
> Django < 3.2.
> With Django >= 3.2 (including 4.0.4) it fails with:
> ValueError: bulk_create() prohibited to prevent data loss due to unsaved
> related object 'reporter'.
>
> Find attached a test app which reproduces this issue with a minimal
> example, see
> mysite/polls/models.py and mysite/polls/tests.py
New description:
Creating both objects of a many-to-one relation using bulk_create together
with ignore_conflicts=True fails since Django >=3.2!
To enable many-to-one relations in bulk_create the "to_field" of the
ForeignKey is defined via a UUIDField with a uuid.UUID4 default value.
Without the ignore_conflicts=True option of bulk_create it works well in
all Django versions.
But if ignore_conflicts=True is needed since some of the objects using in
the bulk_create may already be saved to the DB then it works only for
Django < 3.2.
With Django >= 3.2 (including 4.0.4) it fails with:
`ValueError: bulk_create() prohibited to prevent data loss due to unsaved
related object 'reporter'`.
Find attached a test app which reproduces this issue with a minimal
example, see
mysite/polls/models.py and mysite/polls/tests.py
--
Comment:
Thanks for this report. This behavior was intentionally changed in
10f8b82d195caa3745ba37d9424893763f89653e to prevent possible data loss.
Using `ignore_conflicts=True` tells the database to ignore failure to
insert any rows that fail constraints such as duplicate unique values, but
also disables setting the primary key on each model instance (as
[https://docs.djangoproject.com/en/4.0/ref/models/querysets/#django.db.models.query.QuerySet.bulk_create
documented]). Therefore a `reporter` instance doesn't have a primary key
set. You need to attached already saved `reporter` to the `article` before
`bulk_create()`.
--
Ticket URL: <https://code.djangoproject.com/ticket/33649#comment:1>
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/010701803b0dfd82-67c8dd85-bb40-4b49-accc-b3f3fa6ee4a0-000000%40eu-central-1.amazonses.com.