[Django] #32190: Support for model relationships defined pre-save

9 views
Skip to first unread message

Django

unread,
Nov 12, 2020, 5:04:57 PM11/12/20
to django-...@googlegroups.com
#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.

Django

unread,
Nov 12, 2020, 11:57:40 PM11/12/20
to django-...@googlegroups.com
#32190: Support for model relationships defined pre-save
-------------------------------------+-------------------------------------
Reporter: Ryan Vinzent | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* type: New feature => Bug
* resolution: => duplicate


Comment:

Duplicate of #29497. It was fixed in
10f8b82d195caa3745ba37d9424893763f89653e (Django 3.2+).

--
Ticket URL: <https://code.djangoproject.com/ticket/32190#comment:1>

Reply all
Reply to author
Forward
0 new messages