[Django] #37175: Add database backend methods to get hardcoded or nonexistent primary key values for tests

2 views
Skip to first unread message

Django

unread,
Jun 16, 2026, 5:30:27 PM (8 days ago) Jun 16
to django-...@googlegroups.com
#37175: Add database backend methods to get hardcoded or nonexistent primary key
values for tests
-------------------------------------+-------------------------------------
Reporter: Tim | Owner: Tim Graham
Graham |
Type: New | Status: assigned
feature |
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 |
-------------------------------------+-------------------------------------
While hardcoding primary key values in the Django test suite is avoided as
much as possible, sometimes there are legitimate uses. The existing
hardcoded integer values, however, don't work on databases that don't use
integer primary key values. For example, MongoDB uses `DEFAULT_AUTO_FIELD
= "django_mongodb_backend.fields.ObjectIdAutoField"`.

I propose adding new methods `DatabaseOperations.get_hardcoded_pk()` and
`get_nonexistentd_pk()` to allow a database backend to provide suitable
values, minimizing the size of the Django fork needed for running the test
suite on such databases.
--
Ticket URL: <https://code.djangoproject.com/ticket/37175>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 16, 2026, 5:48:06 PM (8 days ago) Jun 16
to django-...@googlegroups.com
#37175: Add database backend methods to get hardcoded or nonexistent primary key
values for tests
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Tim
| Graham
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* has_patch: 0 => 1

Comment:

[https://github.com/django/django/pull/21497 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/37175#comment:1>

Django

unread,
9:00 AM (3 hours ago) 9:00 AM
to django-...@googlegroups.com
#37175: Add database backend methods to get hardcoded or nonexistent primary key
values for tests
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Tim
| Graham
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* cc: Simon Charette (added)
* stage: Unreviewed => Accepted

Comment:

Hello Tim! Thank you for taking the time to create the report. I
understand your concerns and the issue that this raises for other
backends. I agree we could improve this situation by providing.
potentially, some hooks at the database backend layer, but I also think
that many of the tests currently hardcoding the PKs could be re-worked to
not use a PK at all, or at least not assume is an int.

Right now we are prioritizing 6.1 beta work, but I'll commit to do a more
informed triage/review of this ticket after beta (likely next week). I'm
accepting it right now since the problem statement is real, but I'd like
to evaluate other options for the solution, ideally a solution that would
not require a `get_hardcoded_pk()`. Have you considered posting in the
forum to see what our creative community can suggest?

Thanks for your patience!
--
Ticket URL: <https://code.djangoproject.com/ticket/37175#comment:2>
Reply all
Reply to author
Forward
0 new messages