Opened 5 years ago

Closed 5 years ago

#31297 closed Bug (duplicate)

force_insert not respected by parent class with default primary key

Reported by: Abhijeet Viswa Owned by: Abhijeet Viswa
Component: Database layer (models, ORM) Version: 3.0
Severity: Normal Keywords: force_insert mti
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Abhijeet Viswa)

Consider the following models:

class A(models.Model):
    uuid = models.UUIDField(primary_key=True, default=uuid.uuid4)
    test = models.IntegerField()


class B(A):
    test2 = models.IntegerField()
    pass
b = B()
b.test = 10
b.test2 = 20
b.save(force_insert=True)

will generate the following update/insert statements for PostgreSQL:

UPDATE "save_force_insert_a"
SET "test" = 10
WHERE "save_force_insert_a"."uuid" = '8275d9e0-c0fd-4489-b987-44b9dbe379b4'::UUID;


INSERT INTO "save_force_insert_a" ("uuid",
                                   "test")
VALUES ('8275d9e0-c0fd-4489-b987-44b9dbe379b4'::UUID, 10);


INSERT INTO "save_force_insert_b" ("a_ptr_id",
                                   "test2")
VALUES ('8275d9e0-c0fd-4489-b987-44b9dbe379b4'::UUID, 20);

It's the same even when saving a new instance of a model and the user provides the UUID.

However, in the Model Instance Reference, it says the following:

Changed in Django 3.0:
Model.save() no longer attempts to find a row when saving a new Model instance and a default value for the primary key is provided, and always executes an INSERT.

It is expected that using a force_insert (and value is provided explicitly or as a default), force_insert should be respected by the parents as well.
This isn't fully related to #18305. I believe that the ticket also talks about AutoField PKs.

Change History (3)

comment:1 by ChillarAnand, 5 years ago

Description: modified (diff)

comment:2 by Abhijeet Viswa, 5 years ago

Description: modified (diff)

It seems my description of the ticket was poorly formatted. Thanks @ChillarAnand for fixing it.

comment:3 by Carlton Gibson, 5 years ago

Resolution: duplicate
Status: assignedclosed

Thanks for the report.

The force_insert is not needed in the example. The results are the same with and without it.

The example is identical to #29129. We'll discuss there.

Note: See TracTickets for help on using tickets.
Back to Top