diff options
| author | Clifford Gama <cliffygamy@gmail.com> | 2025-11-10 13:28:07 +0200 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2025-12-05 09:35:15 -0500 |
| commit | 55888655a269f41025405b9a1bff14117ae58e2a (patch) | |
| tree | e880b1a9c51c0d97c0baf6ae25bb348776e68527 /tests/backends | |
| parent | 16252e690720edace3e0e220b99d9141fe091fb4 (diff) | |
Fixed #36722 -- Moved AutoFieldMixin validate_autopk_value() check to get_db_prep_save.
The validation in validate_autopk_value is specific to saving. Having it in
get_db_prep_value caused Value(0, AutoField()) to fail unexpectedly when used
in a filter on MySQL.
Thanks Jacob Walls for the review.
Diffstat (limited to 'tests/backends')
| -rw-r--r-- | tests/backends/tests.py | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/tests/backends/tests.py b/tests/backends/tests.py index 3e708401c5..d53cfa5136 100644 --- a/tests/backends/tests.py +++ b/tests/backends/tests.py @@ -19,6 +19,7 @@ from django.db import ( from django.db.backends.base.base import BaseDatabaseWrapper from django.db.backends.signals import connection_created from django.db.backends.utils import CursorWrapper +from django.db.models import BigAutoField, Value from django.db.models.sql.constants import CURSOR from django.test import ( TestCase, @@ -966,7 +967,7 @@ class ThreadTests(TransactionTestCase): connection.dec_thread_sharing() -class MySQLPKZeroTests(TestCase): +class MySQLAutoPKZeroTests(TestCase): """ Zero as id for AutoField should raise exception in MySQL, because MySQL does not allow zero for autoincrement primary key if the @@ -975,8 +976,15 @@ class MySQLPKZeroTests(TestCase): @skipIfDBFeature("allows_auto_pk_0") def test_zero_as_autoval(self): - with self.assertRaises(ValueError): + msg = "The database backend does not accept 0 as a value for AutoField." + with self.assertRaisesMessage(ValueError, msg): Square.objects.create(id=0, root=0, square=1) + with self.assertRaisesMessage(ValueError, msg): + Square.objects.create(id=Value(0, BigAutoField()), root=0, square=1) + + @skipIfDBFeature("allows_auto_pk_0") + def test_no_error_when_filtering_with_expression(self): + self.assertSequenceEqual(Square.objects.filter(id=Value(0, BigAutoField())), ()) class DBConstraintTestCase(TestCase): |
