Opened 16 months ago

Closed 16 months ago

Last modified 16 months ago

#34242 closed Cleanup/optimization (fixed)

Behavior of Model.delete() with regards to primary_key fields is not documented

Reported by: SwampFalc Owned by: noFFENSE
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

When .delete() is called on a model instance, the primary_key field of that instance is set to None (https://github.com/django/django/blob/main/django/db/models/deletion.py#L456).

For a "standard" autoincrement PK, this is fine. However, if you have defined an actual data field as primary_key on the model, this data is lost in the instance.

This is counter to what is stated in the basic documentation about "Deleting objects" (https://docs.djangoproject.com/en/4.1/ref/models/instances/#deleting-objects): "This only deletes the object in the database; the Python instance will still exist and will still have data in its fields."

There is also no mention of this in the documentation of "primary_key" (https://docs.djangoproject.com/en/4.1/ref/models/fields/#primary-key) or the more in-depth documentation about "Deleting objects" (https://docs.djangoproject.com/en/4.1/topics/db/queries/#deleting-objects).

I feel that this should at the very least be mentioned in the "primary_key" documentation, perhaps in some of the other locations too.

If an agreement can be reached about where it should be mentioned and where not, I am willing to create a pull request.

Change History (13)

comment:1 by SwampFalc, 16 months ago

You know, thinking about it some more... What exactly is the reason for setting the PK to None upon deletion? Is that really necessary?

Should it perhaps only happen when the PK is an autoincrement? Or should it perhaps not happen at all?

comment:2 by Carlton Gibson, 16 months ago

Triage Stage: UnreviewedAccepted

I think something as small as... "the Python instance will still exist and will still have data in its fields, but the primary key is set to None" in the Deleting objects docs you point to, would likely be enough.

You know, thinking about it some more... What exactly is the reason for setting the PK to None upon deletion? Is that really necessary?

Standardly, whether the object has a PK is the test whether it's been saved (i.e. exists in the DB). I doubt that's changeable.
(Others may give a better account.)

comment:3 by noFFENSE, 16 months ago

Owner: changed from nobody to noFFENSE
Status: newassigned

comment:4 by noFFENSE, 16 months ago

Has patch: set

comment:5 by noFFENSE, 16 months ago

Triage Stage: AcceptedReady for checkin

comment:6 by noFFENSE, 16 months ago

Please, tell me if something is wrong, and I will fix it, that's only my 2nd contribution, so I do not know whether I did a good job or not

comment:7 by Mariusz Felisiak, 16 months ago

Triage Stage: Ready for checkinAccepted

Please don't mark your own PRs as "Ready for checkin".

in reply to:  7 comment:8 by noFFENSE, 16 months ago

Replying to Mariusz Felisiak:

Please don't mark your own PRs as "Ready for check-in".

Okay, may I ask why?

comment:9 by Carlton Gibson, 16 months ago

Have a read of the Triage Workflow docs — they explain the process.

(Basically, someone else needs to review your patch first.)

comment:10 by Mariusz Felisiak, 16 months ago

Triage Stage: AcceptedReady for checkin

comment:11 by Mariusz Felisiak <felisiak.mariusz@…>, 16 months ago

Resolution: fixed
Status: assignedclosed

In 5cd13853:

Fixed #34242 -- Doc'd that primary key is set to None when deleting objects.

comment:12 by Mariusz Felisiak <felisiak.mariusz@…>, 16 months ago

In fa52bb7:

[4.2.x] Fixed #34242 -- Doc'd that primary key is set to None when deleting objects.

Backport of 5cd1385356d4b275133ddb0f8c78b2f37c5901eb from main

comment:13 by Mariusz Felisiak <felisiak.mariusz@…>, 16 months ago

In 2a32d39:

[4.1.x] Fixed #34242 -- Doc'd that primary key is set to None when deleting objects.

Backport of 5cd1385356d4b275133ddb0f8c78b2f37c5901eb from main

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