[Django] #34549: Extra select query when parent model consists of primary key only

9 views
Skip to first unread message

Django

unread,
May 8, 2023, 9:31:01 PM5/8/23
to django-...@googlegroups.com
#34549: Extra select query when parent model consists of primary key only
-------------------------------------+-------------------------------------
Reporter: Akash | Owner: nobody
Kumar Sen |
Type: Bug | Status: new
Component: Database | Version: dev
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 |
-------------------------------------+-------------------------------------
Capturing one extra select query instead of update as expected in #30382
when the parent model contains the primary key field only.

{{{
1. SELECT 1 AS "a" FROM "force_insert_update_parentmodel" WHERE
"force_insert_update_parentmodel"."id" = 1 LIMIT 1
2. INSERT INTO "force_insert_update_parentmodel" ("id") VALUES (1)
3. INSERT INTO "force_insert_update_childmodel" ("parentmodel_ptr_id")
VALUES (1)
}}}

Reproduced here : https://code.djangoproject.com/ticket/30382#comment:9

--
Ticket URL: <https://code.djangoproject.com/ticket/34549>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 8, 2023, 9:36:33 PM5/8/23
to django-...@googlegroups.com
#34549: Extra select query when parent model consists of primary key only
-------------------------------------+-------------------------------------
Reporter: Akash Kumar Sen | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: dev
(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
-------------------------------------+-------------------------------------

Comment (by Akash Kumar Sen):

**Case 1 :** The parent model consists of the primary key only. (ref :
https://code.djangoproject.com/ticket/30382#comment:9)

The {{{models.py}}}

{{{
from django.db import models

class ParentModel(models.Model):
id = models.BigIntegerField(primary_key=True)


class ChildModel(ParentModel):
pass
}}}

The testcase


{{{
class Ticke30382TestCase(TestCase):
def test_force_insert_save(self):
with self.assertNumQueries(0):
ChildModel(id=1).save(force_insert=True)
}}}

Queries Captured


{{{
1. SELECT 1 AS "a" FROM "force_insert_update_parentmodel" WHERE
"force_insert_update_parentmodel"."id" = 1 LIMIT 1
2. INSERT INTO "force_insert_update_parentmodel" ("id") VALUES (1)
3. INSERT INTO "force_insert_update_childmodel" ("parentmodel_ptr_id")
VALUES (1)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34549#comment:1>

Django

unread,
May 8, 2023, 9:39:56 PM5/8/23
to django-...@googlegroups.com
#34549: Extra select query when parent model consists of primary key only
-------------------------------------+-------------------------------------
Reporter: Akash Kumar Sen | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: dev
(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
-------------------------------------+-------------------------------------

Comment (by Akash Kumar Sen):

**Case 2 :** The parent model consists of other fields along with primary
key

The {{{models.py}}}

{{{
from django.db import models

class ParentModel(models.Model):
id = models.BigIntegerField(primary_key=True)

name = models.CharField(max_length=10)
value = models.IntegerField()

class ChildModel(ParentModel):
pass
}}}

The testcase


{{{
class Ticke30382TestCase(TestCase):
def test_force_insert_save(self):
with self.assertNumQueries(0):

ChildModel(id=1, name="Akash",
value=5).save(force_insert=True)
}}}

Queries Captured


{{{
1. UPDATE "force_insert_update_parentmodel" SET "name" = 'Akash', "value"
= 5 WHERE "force_insert_update_parentmodel"."id" = 1
2. INSERT INTO "force_insert_update_parentmodel" ("id", "name", "value")
VALUES (1, 'Akash', 5)


3. INSERT INTO "force_insert_update_childmodel" ("parentmodel_ptr_id")
VALUES (1)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34549#comment:2>

Django

unread,
May 8, 2023, 11:27:30 PM5/8/23
to django-...@googlegroups.com
#34549: Extra select query when parent model consists of primary key only
-------------------------------------+-------------------------------------
Reporter: Akash Kumar Sen | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate


Comment:

As far as I'm aware this is also caused by not passing `force_insert`.
Duplicate of #30382.

--
Ticket URL: <https://code.djangoproject.com/ticket/34549#comment:3>

Reply all
Reply to author
Forward
0 new messages