#36722: MySQL backend raises error when selecting 0 using an expression targeting
an autofield.
-------------------------------------+-------------------------------------
Reporter: Clifford Gama | Owner: Clifford
| Gama
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: allows_auto_pk_0 | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Clifford Gama):
My thinking was that since the validation is save-specific, it should be
in `get_db_prep_save()`, which is specific to save, instead of
`get_db_prep_value()` which is also used in filters. I only used `Value(0,
AutoField())` as a testing strategy for the change, since it helped me
identify the issue. If it turns out that there's no other use-case where
filtering zero on an AutoField raises the ValueError, then perhaps this
ticket would become a cleanup/otimisation instead of a bug.
For further context:
The issue discovered in #36689 after I made `ExpressionList` wrap direct
values of an iterable rhs in `Value()` using an explicit `output_field`
([
https://github.com/django/django/pull/20009/commits/0c56236dc2b5526044fa326fde8d52ab084b63de
commit]). The test in the log failed because it has
`filter=Q(id__in=[F("max_book_author"), 0])`, which, when wrapped in an
`ExpressionList`, was becoming `ExpressionList(F("max_book_author"),
Value(0, BigAutoField()))`.
--
Ticket URL: <
https://code.djangoproject.com/ticket/36722#comment:2>