#34634: Creating objects with nested MTI crashes.
-------------------------------------+-------------------------------------
     Reporter:  Mariusz Felisiak     |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  4.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Mariusz Felisiak:

Old description:

> Checking [https://github.com/django/django/pull/16903 PR] I noticed that
> creating objects with more complicated multi-table inheritance crashes.
> For example:
> {{{#!diff
> diff --git a/tests/model_inheritance/models.py
> b/tests/model_inheritance/models.py
> index dc0e238f7e..d75df0d533 100644
> --- a/tests/model_inheritance/models.py
> +++ b/tests/model_inheritance/models.py
> @@ -106,6 +106,10 @@ class ItalianRestaurant(Restaurant):
>      serves_gnocchi = models.BooleanField(default=False)
>

> +class ItalianRestaurantManyParents(ItalianRestaurant, Place):
> +    pass
> +
> +
>  class Supplier(Place):
>      customers = models.ManyToManyField(Restaurant,
> related_name="provider")
>
> diff --git a/tests/model_inheritance/tests.py
> b/tests/model_inheritance/tests.py
> index 4542e6c3cc..838d35b9d6 100644
> --- a/tests/model_inheritance/tests.py
> +++ b/tests/model_inheritance/tests.py
> @@ -14,6 +14,7 @@ from .models import (
>      GrandChild,
>      GrandParent,
>      ItalianRestaurant,
> +    ItalianRestaurantManyParents,
>      MixinModel,
>      Parent,
>      ParkingLot,
> @@ -149,6 +150,13 @@ class ModelInheritanceTests(TestCase):
>          # accidentally found).
>          self.assertSequenceEqual(s.titles.all(), [])
>
> +    def test_create_diamond_mti_common_parents(self):
> +        with self.assertNumQueries(4):
> +            ItalianRestaurantManyParents.objects.create(
> +                name="Ristorante Miron",
> +                address="1234 W. Ash",
> +            )
> +
>      def test_update_parent_filtering(self):
>          """
>          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'
> }}}

New description:

 Checking [https://github.com/django/django/pull/16903 PR] I noticed that
 creating objects with more complicated multi-table inheritance crashes.
 For example:
 [[Image(Screenshot_20230606_091439.png)]]
 {{{#!diff
 diff --git a/tests/model_inheritance/models.py
 b/tests/model_inheritance/models.py
 index dc0e238f7e..d75df0d533 100644
 --- a/tests/model_inheritance/models.py
 +++ b/tests/model_inheritance/models.py
 @@ -106,6 +106,10 @@ class ItalianRestaurant(Restaurant):
      serves_gnocchi = models.BooleanField(default=False)


 +class ItalianRestaurantManyParents(ItalianRestaurant, Place):
 +    pass
 +
 +
  class Supplier(Place):
      customers = models.ManyToManyField(Restaurant,
 related_name="provider")

 diff --git a/tests/model_inheritance/tests.py
 b/tests/model_inheritance/tests.py
 index 4542e6c3cc..838d35b9d6 100644
 --- a/tests/model_inheritance/tests.py
 +++ b/tests/model_inheritance/tests.py
 @@ -14,6 +14,7 @@ from .models import (
      GrandChild,
      GrandParent,
      ItalianRestaurant,
 +    ItalianRestaurantManyParents,
      MixinModel,
      Parent,
      ParkingLot,
 @@ -149,6 +150,13 @@ class ModelInheritanceTests(TestCase):
          # accidentally found).
          self.assertSequenceEqual(s.titles.all(), [])

 +    def test_create_diamond_mti_common_parents(self):
 +        with self.assertNumQueries(4):
 +            ItalianRestaurantManyParents.objects.create(
 +                name="Ristorante Miron",
 +                address="1234 W. Ash",
 +            )
 +
      def test_update_parent_filtering(self):
          """
          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'
 }}}

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34634#comment:1>
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/010701888f8f9d0c-453eb16e-d278-43f6-8cd0-505ddc437489-000000%40eu-central-1.amazonses.com.

Reply via email to