[Django] #34242: Behavior of Model.delete() with regards to primary_key fields is not documented

10 views
Skip to first unread message

Django

unread,
Jan 5, 2023, 5:09:35 AM1/5/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
------------------------------------------------+------------------------
Reporter: SwampFalc | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: dev
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
------------------------------------------------+------------------------
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.

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

Django

unread,
Jan 5, 2023, 5:28:34 AM1/5/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
-------------------------------------+-------------------------------------
Reporter: SwampFalc | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: dev
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by SwampFalc):

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?

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

Django

unread,
Jan 5, 2023, 7:26:07 AM1/5/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
-------------------------------------+-------------------------------------
Reporter: SwampFalc | Owner:
Type: | santhosh_reddy
Cleanup/optimization | Status: assigned

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by santhosh_reddy):

* owner: nobody => santhosh_reddy
* status: new => assigned


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

Django

unread,
Jan 5, 2023, 7:30:02 AM1/5/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
-------------------------------------+-------------------------------------
Reporter: SwampFalc | Owner: (none)
Type: | Status: new
Cleanup/optimization |

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by santhosh_reddy):

* owner: santhosh_reddy => (none)
* status: assigned => new


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

Django

unread,
Jan 10, 2023, 2:23:42 AM1/10/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
--------------------------------------+------------------------------------
Reporter: SwampFalc | Owner: nobody
Type: Cleanup/optimization | Status: new

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Carlton Gibson):

* stage: Unreviewed => Accepted


Comment:

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 [https://docs.djangoproject.com/en/4.1/ref/models/instances
/#deleting-objects 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.)

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

Django

unread,
Jan 14, 2023, 1:39:31 AM1/14/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
-------------------------------------+-------------------------------------
Reporter: SwampFalc | Owner:
Type: | AshirRashid
Cleanup/optimization | Status: assigned

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by AshirRashid):

* owner: nobody => AshirRashid


* status: new => assigned


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

Django

unread,
Jan 17, 2023, 1:28:39 AM1/17/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
--------------------------------------+------------------------------------
Reporter: SwampFalc | Owner: (none)
Type: Cleanup/optimization | Status: new

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Ashir Rashid):

* owner: Ashir Rashid => (none)


* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:4>

Django

unread,
Jan 17, 2023, 12:38:53 PM1/17/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
--------------------------------------+------------------------------------
Reporter: SwampFalc | Owner: noFFENSE
Type: Cleanup/optimization | Status: assigned

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by noFFENSE):

* owner: nobody => noFFENSE


* status: new => assigned


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

Django

unread,
Jan 18, 2023, 5:59:38 AM1/18/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
--------------------------------------+------------------------------------
Reporter: SwampFalc | Owner: noFFENSE
Type: Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by noFFENSE):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:4>

Django

unread,
Jan 18, 2023, 6:04:57 AM1/18/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
-------------------------------------+-------------------------------------
Reporter: SwampFalc | Owner: noFFENSE
Type: | Status: assigned
Cleanup/optimization |

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by noFFENSE):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:5>

Django

unread,
Jan 18, 2023, 6:06:01 AM1/18/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
-------------------------------------+-------------------------------------
Reporter: SwampFalc | Owner: noFFENSE
Type: | Status: assigned
Cleanup/optimization |
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by noFFENSE):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:6>

Django

unread,
Jan 18, 2023, 8:10:39 AM1/18/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
--------------------------------------+------------------------------------
Reporter: SwampFalc | Owner: noFFENSE
Type: Cleanup/optimization | Status: assigned

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

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

* stage: Ready for checkin => Accepted


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:7>

Django

unread,
Jan 18, 2023, 8:13:42 AM1/18/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
--------------------------------------+------------------------------------
Reporter: SwampFalc | Owner: noFFENSE
Type: Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by noFFENSE):

Replying to [comment:7 Mariusz Felisiak]:
> Please don't mark your own PRs as "Ready for check-in".

Okay, may I ask why?

--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:8>

Django

unread,
Jan 18, 2023, 8:15:20 AM1/18/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
--------------------------------------+------------------------------------
Reporter: SwampFalc | Owner: noFFENSE
Type: Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Carlton Gibson):

Have a read of the
[https://docs.djangoproject.com/en/dev/internals/contributing/triaging-
tickets/#triage-workflow Triage Workflow docs] — they explain the process.

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

--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:9>

Django

unread,
Jan 19, 2023, 1:20:45 PM1/19/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
-------------------------------------+-------------------------------------
Reporter: SwampFalc | Owner: noFFENSE
Type: | Status: assigned
Cleanup/optimization |

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

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

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:10>

Django

unread,
Jan 20, 2023, 4:57:32 AM1/20/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
-------------------------------------+-------------------------------------
Reporter: SwampFalc | Owner: noFFENSE
Type: | Status: closed

Cleanup/optimization |
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"5cd1385356d4b275133ddb0f8c78b2f37c5901eb" 5cd13853]:
{{{
#!CommitTicketReference repository=""
revision="5cd1385356d4b275133ddb0f8c78b2f37c5901eb"
Fixed #34242 -- Doc'd that primary key is set to None when deleting
objects.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:11>

Django

unread,
Jan 20, 2023, 4:57:50 AM1/20/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
-------------------------------------+-------------------------------------
Reporter: SwampFalc | Owner: noFFENSE
Type: | Status: closed
Cleanup/optimization |
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"fa52bb77747be33dc9dfb07c26307e60958adaaa" fa52bb7]:
{{{
#!CommitTicketReference repository=""
revision="fa52bb77747be33dc9dfb07c26307e60958adaaa"
[4.2.x] Fixed #34242 -- Doc'd that primary key is set to None when
deleting objects.

Backport of 5cd1385356d4b275133ddb0f8c78b2f37c5901eb from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:12>

Django

unread,
Jan 20, 2023, 4:58:10 AM1/20/23
to django-...@googlegroups.com
#34242: Behavior of Model.delete() with regards to primary_key fields is not
documented
-------------------------------------+-------------------------------------
Reporter: SwampFalc | Owner: noFFENSE
Type: | Status: closed
Cleanup/optimization |
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"2a32d3963c974b605050688dacfe2a6ae76f52e8" 2a32d39]:
{{{
#!CommitTicketReference repository=""
revision="2a32d3963c974b605050688dacfe2a6ae76f52e8"
[4.1.x] Fixed #34242 -- Doc'd that primary key is set to None when
deleting objects.

Backport of 5cd1385356d4b275133ddb0f8c78b2f37c5901eb from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:13>

Reply all
Reply to author
Forward
0 new messages