Fixed #24675 -- Skipped SQL_AUTO_IS_NULL query on MySQL if not needed.
This commit is contained in:
parent
3d2236773b
commit
b7fdd60d85
|
@ -267,12 +267,13 @@ class DatabaseWrapper(BaseDatabaseWrapper):
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
def init_connection_state(self):
|
def init_connection_state(self):
|
||||||
with self.cursor() as cursor:
|
if self.features.is_sql_auto_is_null_enabled:
|
||||||
# SQL_AUTO_IS_NULL in MySQL controls whether an AUTO_INCREMENT column
|
with self.cursor() as cursor:
|
||||||
# on a recently-inserted row will return when the field is tested for
|
# SQL_AUTO_IS_NULL controls whether an AUTO_INCREMENT column on
|
||||||
# NULL. Disabling this value brings this aspect of MySQL in line with
|
# a recently inserted row will return when the field is tested
|
||||||
# SQL standards.
|
# for NULL. Disabling this brings this aspect of MySQL in line
|
||||||
cursor.execute('SET SQL_AUTO_IS_NULL = 0')
|
# with SQL standards.
|
||||||
|
cursor.execute('SET SQL_AUTO_IS_NULL = 0')
|
||||||
|
|
||||||
def create_cursor(self):
|
def create_cursor(self):
|
||||||
cursor = self.connection.cursor()
|
cursor = self.connection.cursor()
|
||||||
|
|
|
@ -69,3 +69,9 @@ class DatabaseFeatures(BaseDatabaseFeatures):
|
||||||
|
|
||||||
def introspected_boolean_field_type(self, *args, **kwargs):
|
def introspected_boolean_field_type(self, *args, **kwargs):
|
||||||
return 'IntegerField'
|
return 'IntegerField'
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def is_sql_auto_is_null_enabled(self):
|
||||||
|
with self.connection.cursor() as cursor:
|
||||||
|
cursor.execute('SELECT @@SQL_AUTO_IS_NULL')
|
||||||
|
return cursor.fetchone()[0] == 1
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from django.db import connection
|
||||||
|
from django.test import TestCase, override_settings
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(DEBUG=True)
|
||||||
|
@unittest.skipUnless(connection.vendor == 'mysql', 'MySQL specific test.')
|
||||||
|
class MySQLTests(TestCase):
|
||||||
|
|
||||||
|
def test_auto_is_null_auto_config(self):
|
||||||
|
query = 'set sql_auto_is_null = 0'
|
||||||
|
connection.init_connection_state()
|
||||||
|
last_query = connection.queries[-1]['sql'].lower()
|
||||||
|
if connection.features.is_sql_auto_is_null_enabled:
|
||||||
|
self.assertIn(query, last_query)
|
||||||
|
else:
|
||||||
|
self.assertNotIn(query, last_query)
|
Loading…
Reference in New Issue