#32190: Support for model relationships defined pre-save
-------------------------------------+-------------------------------------
               Reporter:  Ryan       |          Owner:  nobody
  Vinzent                            |
                   Type:  New        |         Status:  new
  feature                            |
              Component:  Database   |        Version:  3.1
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 There are many situations where it is optimal to define a bunch of
 objects, and then later commit them in bulk. When the objects define a FK
 relationship, this strategy requires a bit of hacking that I would prefer
 not to do. Take the following example:

 {{{#!python
 class Parent(models.Model):
     name = models.TextField()

 class Child(models.Model):
     name = models.TextField()
     parent = models.ForeignKey(Parent, on_delete=models.RESTRICT)
 }}}

 Now if we have some function that defines a bunch of these objects to
 later commit them in bulk:

 {{{#!python
 def build_objects(parent_child_mapping):
     parents = []
     children = []
     # {"father": ["son", "daughter"], ...}
     for parent_name, child_names in parent_child_mapping.items():
         parent = Parent(name=parent_name)
         parents.append(parent)
         children.extend(Child(parent=parent, name=child_name) for
 child_name in child_names)

     # now commit all objects in bulk
     Parent.objects.bulk_create(parents)

     # fails with IntegrityError: parent_id is not nullable
     Child.objects.bulk_create(children)
 }}}

 I would expect the above to work fine, given the parent ID's are known
 when inserting the children, however this would throw an `IntegrityError`
 because `parent_id` on the `Child` objects is still `None`.

 Adding a simple hack to the function will let it work fine, but it's
 annoying to do this ''everywhere'' where this pattern of creating objects
 is followed.
 {{{#!python
 def build_objects(parent_child_mapping):
     parents = []
     children = []
     for parent_name, child_names in parent_child_mapping.items():
         parent = Parent(name=parent_name)
         parents.append(parent)
         children.extend(Child(parent=parent, name=child_name) for
 child_name in child_names)

     # now commit all objects in bulk
     Parent.objects.bulk_create(parents)

     # add hack to make sure object IDs are properly assigned
     for child in children:
         child.parent_id = child.parent.id

     # now this call can succeed
     Child.objects.bulk_create(children)
 }}}

 This seems like something that should be supported by Django, and  I
 believe this would make the ORM more consistent overall.

 Thanks.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32190>
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/051.391975f17391bcf285a42a5292cb885f%40djangoproject.com.

Reply via email to