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

Comment (by Simon Charette):

 It feels like `ItalianRestaurantManyParents` should not be allowed to be
 defined in the first place since `place_ptr` conflicts.

 For example the following model cannot be defined

 {{{#!diff
 diff --git a/tests/model_inheritance/models.py
 b/tests/model_inheritance/models.py
 index dc0e238f7e..9b25ac4b8a 100644
 --- a/tests/model_inheritance/models.py
 +++ b/tests/model_inheritance/models.py
 @@ -110,6 +110,9 @@ class Supplier(Place):
      customers = models.ManyToManyField(Restaurant,
 related_name="provider")


 +class Foo(Supplier, Restaurant):
 +    pass
 +
  class CustomSupplier(Supplier):
      pass

 }}}

 {{{
 model_inheritance.Bob: (models.E005) The field 'place_ptr' from parent
 model 'model_inheritance.supplier' clashes with the field 'place_ptr' from
 parent model 'model_inheritance.restaurant'.
 }}}

 I would expect a similar crash when defining
 `ItalianRestaurantManyParents` about a conflicting `place_ptr`.

 Once the model is appropriately defined to avoid the conflicting parent
 link the creation succeeds with an extra query

 {{{#!diff
 diff --git a/tests/model_inheritance/models.py
 b/tests/model_inheritance/models.py
 index dc0e238f7e..c303a97d04 100644
 --- a/tests/model_inheritance/models.py
 +++ b/tests/model_inheritance/models.py
 @@ -106,6 +106,12 @@ class ItalianRestaurant(Restaurant):
      serves_gnocchi = models.BooleanField(default=False)


 +class ItalianRestaurantManyParents(ItalianRestaurant, Place):
 +    place_two_ptr = models.OneToOneField(
 +        Place, on_delete=models.CASCADE, parent_link=True
 +    )
 +
 +
  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..0ed9d2f14e 100644
 --- a/tests/model_inheritance/tests.py
 +++ b/tests/model_inheritance/tests.py
 @@ -25,6 +25,7 @@
      Supplier,
      Title,
      Worker,
 +    ItalianRestaurantManyParents,
  )


 @@ -149,6 +150,13 @@ def test_custompk_m2m(self):
          # accidentally found).
          self.assertSequenceEqual(s.titles.all(), [])

 +    def test_create_diamond_mti_common_parents(self):
 +        with self.assertNumQueries(5):
 +            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
 }}}

 {{{
 1. INSERT INTO "model_inheritance_place" ("name", "address") VALUES ('',
 '') RETURNING "model_inheritance_place"."id"
 2. INSERT INTO "my_restaurant" ("place_ptr_id", "rating",
 "serves_hot_dogs", "serves_pizza", "chef_id") VALUES (1, NULL, 0, 0, NULL)
 3. INSERT INTO "model_inheritance_italianrestaurant" ("restaurant_ptr_id",
 "serves_gnocchi") VALUES (1, 0)
 4. UPDATE "model_inheritance_place" SET "name" = '', "address" = '' WHERE
 "model_inheritance_place"."id" = 1
 5. INSERT INTO "model_inheritance_italianrestaurantmanyparents"
 ("italianrestaurant_ptr_id", "place_two_ptr_id") VALUES (1, 1)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34634#comment:10>
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/01070188b85f16da-d3e7ae71-d0bd-4f0c-b60e-9fb9b2f73516-000000%40eu-central-1.amazonses.com.

Reply via email to