#30382: force_insert flag is not passed when saving parents on inherited models.
-------------------------------------+-------------------------------------
     Reporter:  Phill Tornroth       |                    Owner:  Akash
                                     |  Kumar Sen
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Natalia Bidart):

 Thank you Akash, your comment helped me reproduce the reported issue. In
 summary, a Parent model with at least two fields, `id` and `name` for
 example, would generate these queries for
 `ChildModel(id=1).save(force_insert=True)`:

 {{{
 1. UPDATE "ticket_30382_parentmodel" SET "name" = '' WHERE
 "ticket_30382_parentmodel"."id" = 1
 2. INSERT INTO "ticket_30382_parentmodel" ("id", "name") VALUES (1, '')
 3. INSERT INTO "ticket_30382_childmodel" ("parentmodel_ptr_id") VALUES (1)
 }}}

 I also added a test for the alternate code:

 {{{
 parent = ParentModel.objects.create(id=1)
 ChildModel.objects.create(parentmodel_ptr=parent)
 }}}

 which generates these queries:
 {{{
 1. INSERT INTO "ticket_30382_parentmodel" ("id", "name") VALUES (1, '')
 2. UPDATE "ticket_30382_parentmodel" SET "name" = '' WHERE
 "ticket_30382_parentmodel"."id" = 1
 3. INSERT INTO "ticket_30382_childmodel" ("parentmodel_ptr_id") VALUES (1)
 }}}

 When testing this same code with the **proposed PR**, **the first case
 gets fixed as expected**, these are the queries I get for the reported
 code (so YEY):

 {{{
 1. INSERT INTO "ticket_30382_parentmodel" ("id", "name") VALUES (1, '')
 2. INSERT INTO "ticket_30382_childmodel" ("parentmodel_ptr_id") VALUES (1)
 }}}

 **But** for the second code snippet (create parent and then create child)
 I get an **extra query** (so HUM). Is this expected? I wouldn't think
 so...
 {{{
 1. INSERT INTO "ticket_30382_parentmodel" ("id", "name") VALUES (1, '')
 2. UPDATE "ticket_30382_parentmodel" SET "name" = '' WHERE
 "ticket_30382_parentmodel"."id" = 1
 3. SELECT 1 AS "a" FROM "ticket_30382_childmodel" WHERE
 "ticket_30382_childmodel"."parentmodel_ptr_id" = 1 LIMIT 1
 4. INSERT INTO "ticket_30382_childmodel" ("parentmodel_ptr_id") VALUES (1)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30382#comment:11>
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/0107018800c79c7a-74865cf9-426c-4461-968d-f9861207e2d7-000000%40eu-central-1.amazonses.com.

Reply via email to