Opened 19 months ago

Last modified 18 months ago

#34634 closed Bug

Creating objects with nested MTI crashes. — at Initial Version

Reported by: Mariusz Felisiak Owned by: nobody
Component: Database layer (models, ORM) Version: 4.2
Severity: Normal Keywords:
Cc: Simon Charette, David Sanders, Lily Foote Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Checking PR I noticed that creating objects with more complicated multi-table inheritance crashes. For example:

  • tests/model_inheritance/models.py

    diff --git a/tests/model_inheritance/models.py b/tests/model_inheritance/models.py
    index dc0e238f7e..d75df0d533 100644
    a b class ItalianRestaurant(Restaurant):  
    106106    serves_gnocchi = models.BooleanField(default=False)
    107107
    108108
     109class ItalianRestaurantManyParents(ItalianRestaurant, Place):
     110    pass
     111
     112
    109113class Supplier(Place):
    110114    customers = models.ManyToManyField(Restaurant, related_name="provider")
    111115
  • tests/model_inheritance/tests.py

    diff --git a/tests/model_inheritance/tests.py b/tests/model_inheritance/tests.py
    index 4542e6c3cc..838d35b9d6 100644
    a b from .models import (  
    1414    GrandChild,
    1515    GrandParent,
    1616    ItalianRestaurant,
     17    ItalianRestaurantManyParents,
    1718    MixinModel,
    1819    Parent,
    1920    ParkingLot,
    class ModelInheritanceTests(TestCase):  
    149150        # accidentally found).
    150151        self.assertSequenceEqual(s.titles.all(), [])
    151152
     153    def test_create_diamond_mti_common_parents(self):
     154        with self.assertNumQueries(4):
     155            ItalianRestaurantManyParents.objects.create(
     156                name="Ristorante Miron",
     157                address="1234 W. Ash",
     158            )
     159
    152160    def test_update_parent_filtering(self):
    153161        """
    154162        Updating a field of a model subclass doesn't issue an UPDATE

crashes with:

  File "/django/tests/model_inheritance/tests.py", line 155, in test_create_diamond_mti_common_parents
    ItalianRestaurantManyParents.objects.create(
  File "/django/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/django/django/db/models/query.py", line 650, in create
    obj.save(force_insert=True, using=self.db)
  File "/django/django/db/models/base.py", line 814, in save
    self.save_base(
  File "/django/django/db/models/base.py", line 876, in save_base
    parent_inserted = self._save_parents(cls, using, update_fields)
  File "/django/django/db/models/base.py", line 928, in _save_parents
    setattr(self, field.attname, self._get_pk_val(parent._meta))
AttributeError: 'OneToOneField' object has no attribute 'attname'

Change History (1)

by Mariusz Felisiak, 19 months ago

Note: See TracTickets for help on using tickets.
Back to Top