#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.

-- 
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/0107018038d99ec4-3984e0cb-e84e-4f68-976d-5d028eb2cf50-000000%40eu-central-1.amazonses.com.

Reply via email to