Fixed #30664 -- Fixed migrations crash when altering table on SQLite or altering AutoField/BigAutoField on PostgreSQL for models with quoted db_table.

This commit is contained in:
Ngalim Siregar 2019-07-31 19:02:15 +07:00 committed by Mariusz Felisiak
parent a5652eb795
commit e4684220af
3 changed files with 22 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import psycopg2
from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.backends.ddl_references import IndexColumns from django.db.backends.ddl_references import IndexColumns
from django.db.backends.utils import strip_quotes
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
@ -67,7 +68,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
if self._field_data_type(old_field) != self._field_data_type(new_field): if self._field_data_type(old_field) != self._field_data_type(new_field):
self.sql_alter_column_type += ' USING %(column)s::%(type)s' self.sql_alter_column_type += ' USING %(column)s::%(type)s'
# Make ALTER TYPE with SERIAL make sense. # Make ALTER TYPE with SERIAL make sense.
table = model._meta.db_table table = strip_quotes(model._meta.db_table)
if new_type.lower() in ("serial", "bigserial"): if new_type.lower() in ("serial", "bigserial"):
column = new_field.column column = new_field.column
sequence_name = "%s_%s_seq" % (table, column) sequence_name = "%s_%s_seq" % (table, column)

View File

@ -4,6 +4,7 @@ from decimal import Decimal
from django.apps.registry import Apps from django.apps.registry import Apps
from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.backends.ddl_references import Statement from django.db.backends.ddl_references import Statement
from django.db.backends.utils import strip_quotes
from django.db.models import UniqueConstraint from django.db.models import UniqueConstraint
from django.db.transaction import atomic from django.db.transaction import atomic
from django.db.utils import NotSupportedError from django.db.utils import NotSupportedError
@ -263,7 +264,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
body_copy = copy.deepcopy(body) body_copy = copy.deepcopy(body)
meta_contents = { meta_contents = {
'app_label': model._meta.app_label, 'app_label': model._meta.app_label,
'db_table': 'new__%s' % model._meta.db_table, 'db_table': 'new__%s' % strip_quotes(model._meta.db_table),
'unique_together': unique_together, 'unique_together': unique_together,
'index_together': index_together, 'index_together': index_together,
'indexes': indexes, 'indexes': indexes,

View File

@ -649,6 +649,24 @@ class SchemaTests(TransactionTestCase):
editor.remove_field(Author, Author._meta.get_field('id')) editor.remove_field(Author, Author._meta.get_field('id'))
editor.alter_field(Author, old_field, new_field, strict=True) editor.alter_field(Author, old_field, new_field, strict=True)
@isolate_apps('schema')
def test_alter_primary_key_quoted_db_table(self):
class Foo(Model):
class Meta:
app_label = 'schema'
db_table = '"foo"'
with connection.schema_editor() as editor:
editor.create_model(Foo)
self.isolated_local_models = [Foo]
old_field = Foo._meta.get_field('id')
new_field = BigAutoField(primary_key=True)
new_field.model = Foo
new_field.set_attributes_from_name('id')
with connection.schema_editor() as editor:
editor.alter_field(Foo, old_field, new_field, strict=True)
Foo.objects.create()
def test_alter_text_field(self): def test_alter_text_field(self):
# Regression for "BLOB/TEXT column 'info' can't have a default value") # Regression for "BLOB/TEXT column 'info' can't have a default value")
# on MySQL. # on MySQL.