Opened 2 years ago

Last modified 2 years ago

#33649 closed Bug

bulk_create with ignore_conflicts=True and ForeignKey fails — at Initial Version

Reported by: Markus Friedrich Owned by: nobody
Component: Database layer (models, ORM) Version: 3.2
Severity: Normal Keywords: bulk_create ForeignKey ignore_conflicts
Cc: Hannes Ljungberg Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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

Change History (1)

by Markus Friedrich, 2 years ago

Attachment: mysite.zip added
Note: See TracTickets for help on using tickets.
Back to Top