[Django] #33649: bulk_create with ignore_conflicts=True and ForeignKey fails

235 views
Skip to first unread message

Django

unread,
Apr 17, 2022, 2:47:11 PM4/17/22
to django-...@googlegroups.com
#33649: bulk_create with ignore_conflicts=True and ForeignKey fails
-------------------------------------+-------------------------------------
Reporter: Markus | Owner: nobody
Friedrich |
Type: Bug | Status: new
Component: | Version: 3.2
Uncategorized | Keywords: bulk_create
Severity: Normal | ForeignKey ignore_conflicts
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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

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

Django

unread,
Apr 17, 2022, 2:48:00 PM4/17/22
to django-...@googlegroups.com
#33649: bulk_create with ignore_conflicts=True and ForeignKey fails
-------------------------------------+-------------------------------------
Reporter: Markus Friedrich | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 3.2
Severity: Normal | Resolution:
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 Markus Friedrich):

* Attachment "mysite.zip" added.

Reply all
Reply to author
Forward
0 new messages