#31301: bulk_create with ForeignKey fields with mixed filled/None values fails
-------------------------------------+-------------------------------------
               Reporter:  Hans       |          Owner:  nobody
  Aarne Liblik                       |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  3.0
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  oracle sql
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 I have a model with ForeignKey (null=True) to another model

 {{{
 class A(models.Model):
   name = models.CharField()

 class B(models.Model):
   name = models.CharField()
   a = models.ForeignKey(A, on_delete=models.CASCADE, null=True)
 }}}

 When I try to use bulk_create as follows

 {{{
 B.objects.bulk_create([
   B(name='first', a_id=1)
   B(name='seconds', a_id=None)
 ])
 }}}

 the query fails with error

 {{{
 django.db.utils.DatabaseError: ORA-01790: expression must have same
 datatype as corresponding expression
 }}}

 I got bulk_create working if the ForeignKey references are either All None
 or All NOT None. It seems like the fix introduced in
 https://code.djangoproject.com/ticket/22669 fixes the case for
 NumberFields and CharFields, but ForeignKey field 'target_field' for me is
 'AutoField' which is not defined in
 {{{
 django/db/backends/oracle/utils.py#52  class BulkInsertMapper:
 }}}
 Manually editing that this file and adding
 {{{
 class BulkInsertMapper:
   ...
   types: {
     ...
     ...
     'AutoField': NUMBER
   }
 }}}

 fixes the issue for me.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31301>
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/058.4358e90d764cd8e3c78bb9097d8b8040%40djangoproject.com.

Reply via email to