1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #17653 -- Allowed using zero as AutoFields value on MySQL if NO_AUTO_VALUE_ON_ZERO SQL mode is enabled.

This commit is contained in:
Mariusz Felisiak
2020-07-20 09:48:31 +02:00
committed by GitHub
parent 730711e828
commit 83f55aafdd
7 changed files with 27 additions and 8 deletions

View File

@@ -100,7 +100,7 @@ class BaseDatabaseFeatures:
# The database's limit on the number of query parameters.
max_query_params = None
# Can an object have an autoincrement primary key of 0? MySQL says No.
# Can an object have an autoincrement primary key of 0?
allows_auto_pk_0 = True
# Do we need to NULL a ForeignKey out, or can the constraint check be

View File

@@ -17,7 +17,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_index_column_ordering = False
supports_timezones = False
requires_explicit_null_ordering_when_grouping = True
allows_auto_pk_0 = False
can_release_savepoints = True
atomic_transactions = False
can_clone_databases = True
@@ -51,6 +50,14 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"Internal method used in Django tests. Don't rely on this from your code"
return self.connection.mysql_server_data['default_storage_engine']
@cached_property
def allows_auto_pk_0(self):
"""
Autoincrement primary key can be set to 0 if it doesn't generate new
autoincrement values.
"""
return 'NO_AUTO_VALUE_ON_ZERO' in self.connection.sql_mode
@cached_property
def update_can_self_select(self):
return self.connection.mysql_is_mariadb and self.connection.mysql_version >= (10, 3, 2)

View File

@@ -227,8 +227,9 @@ class DatabaseOperations(BaseDatabaseOperations):
]
def validate_autopk_value(self, value):
# MySQLism: zero in AUTO_INCREMENT field does not work. Refs #17653.
if value == 0:
# Zero in AUTO_INCREMENT field does not work without the
# NO_AUTO_VALUE_ON_ZERO SQL mode.
if value == 0 and not self.connection.features.allows_auto_pk_0:
raise ValueError('The database backend does not accept 0 as a '
'value for AutoField.')
return value
@@ -266,6 +267,9 @@ class DatabaseOperations(BaseDatabaseOperations):
def max_name_length(self):
return 64
def pk_default_value(self):
return 'NULL'
def bulk_insert_sql(self, fields, placeholder_rows):
placeholder_rows_sql = (", ".join(row) for row in placeholder_rows)
values_sql = ", ".join("(%s)" % sql for sql in placeholder_rows_sql)