{{{
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.
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>
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>
* 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>