#29129: Child model updates parent model with empty fields making an extra query in multi-inheritance use case -------------------------------------+------------------------------------- Reporter: user0007 | Owner: nobody Type: Bug | Status: new Component: Database | Version: 2.0 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 | -------------------------------------+------------------------------------- While creating a new model object (using multi-inheritance model => `Child(Parent)`), Django does an extra update query setting parent model fields to empty values. This situation occurs *only* if we define a custom primary key in a parent model (eg. as an UUID field).
An example *without* custom primary key (correct behavior): {{{ class Parent(models.Model): title = models.TextField() class Child(Parent): body = models.TextField() >> Child.objects.create() 1. INSERT INTO "app_parent" ("title") VALUES ('') RETURNING "app_parent"."id" 2. INSERT INTO "app_child" ("parent_ptr_id", "body") VALUES (1, '') }}} An example *with* custom primary key (incorrect behavior): {{{ class Parent(models.Model): id = models.UUIDField( primary_key=True, default=uuid.uuid4, editable=False ) title = models.TextField() class Child(Parent): body = models.TextField() >> Child.objects.create() 1. UPDATE "app_parent" SET "title" = '' WHERE "app_parent"."id" = 'd750cfdd-ae7b-48a6-a2e0-d49e70e28686'::uuid 2. INSERT INTO "app_parent" ("id", "title") VALUES ('d750cfdd-ae7b- 48a6-a2e0-d49e70e28686'::uuid, '') 3. INSERT INTO "app_child" ("parent_ptr_id", "body") VALUES ('d750cfdd- ae7b-48a6-a2e0-d49e70e28686'::uuid, '') }}} Python 3.6, PostgreSQL 9.6 -- Ticket URL: <https://code.djangoproject.com/ticket/29129> 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 django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/051.810207368c7acc3ec882e1dde5aa4ccb%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.