Refs #28876 -- Fixed incorrect foreign key constraint name for models with quoted db_table.
Thanks Simon Charette and Tim Graham for the review and Carlos E. C. Leite for the report.
This commit is contained in:
parent
f79d9a322c
commit
fc48047586
|
@ -966,7 +966,7 @@ class BaseDatabaseSchemaEditor:
|
||||||
def _create_fk_sql(self, model, field, suffix):
|
def _create_fk_sql(self, model, field, suffix):
|
||||||
from_table = model._meta.db_table
|
from_table = model._meta.db_table
|
||||||
from_column = field.column
|
from_column = field.column
|
||||||
to_table = field.target_field.model._meta.db_table
|
_, to_table = split_identifier(field.target_field.model._meta.db_table)
|
||||||
to_column = field.target_field.column
|
to_column = field.target_field.column
|
||||||
|
|
||||||
def create_fk_name(*args, **kwargs):
|
def create_fk_name(*args, **kwargs):
|
||||||
|
@ -977,8 +977,8 @@ class BaseDatabaseSchemaEditor:
|
||||||
table=Table(from_table, self.quote_name),
|
table=Table(from_table, self.quote_name),
|
||||||
name=ForeignKeyName(from_table, [from_column], to_table, [to_column], suffix, create_fk_name),
|
name=ForeignKeyName(from_table, [from_column], to_table, [to_column], suffix, create_fk_name),
|
||||||
column=Columns(from_table, [from_column], self.quote_name),
|
column=Columns(from_table, [from_column], self.quote_name),
|
||||||
to_table=Table(to_table, self.quote_name),
|
to_table=Table(field.target_field.model._meta.db_table, self.quote_name),
|
||||||
to_column=Columns(to_table, [to_column], self.quote_name),
|
to_column=Columns(field.target_field.model._meta.db_table, [to_column], self.quote_name),
|
||||||
deferrable=self.connection.ops.deferrable_sql(),
|
deferrable=self.connection.ops.deferrable_sql(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -14,3 +14,6 @@ Bugfixes
|
||||||
|
|
||||||
* Fixed incorrect class-based model index name generation for models with
|
* Fixed incorrect class-based model index name generation for models with
|
||||||
quoted ``db_table`` (:ticket:`28876`).
|
quoted ``db_table`` (:ticket:`28876`).
|
||||||
|
|
||||||
|
* Fixed incorrect foreign key constraint name for models with quoted
|
||||||
|
``db_table`` (:ticket:`28876`).
|
||||||
|
|
|
@ -14,3 +14,6 @@ Bugfixes
|
||||||
|
|
||||||
* Fixed incorrect class-based model index name generation for models with
|
* Fixed incorrect class-based model index name generation for models with
|
||||||
quoted ``db_table`` (:ticket:`28876`).
|
quoted ``db_table`` (:ticket:`28876`).
|
||||||
|
|
||||||
|
* Fixed incorrect foreign key constraint name for models with quoted
|
||||||
|
``db_table`` (:ticket:`28876`).
|
||||||
|
|
|
@ -1914,6 +1914,28 @@ class SchemaTests(TransactionTestCase):
|
||||||
with connection.schema_editor() as editor:
|
with connection.schema_editor() as editor:
|
||||||
editor.add_field(BookWithLongName, new_field)
|
editor.add_field(BookWithLongName, new_field)
|
||||||
|
|
||||||
|
@isolate_apps('schema')
|
||||||
|
@skipUnlessDBFeature('supports_foreign_keys')
|
||||||
|
def test_add_foreign_key_quoted_db_table(self):
|
||||||
|
class Author(Model):
|
||||||
|
class Meta:
|
||||||
|
db_table = '"table_author_double_quoted"'
|
||||||
|
app_label = 'schema'
|
||||||
|
|
||||||
|
class Book(Model):
|
||||||
|
author = ForeignKey(Author, CASCADE)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
app_label = 'schema'
|
||||||
|
|
||||||
|
with connection.schema_editor() as editor:
|
||||||
|
editor.create_model(Author)
|
||||||
|
editor.create_model(Book)
|
||||||
|
if connection.vendor == 'mysql':
|
||||||
|
self.assertForeignKeyExists(Book, 'author_id', '"table_author_double_quoted"')
|
||||||
|
else:
|
||||||
|
self.assertForeignKeyExists(Book, 'author_id', 'table_author_double_quoted')
|
||||||
|
|
||||||
def test_add_foreign_object(self):
|
def test_add_foreign_object(self):
|
||||||
with connection.schema_editor() as editor:
|
with connection.schema_editor() as editor:
|
||||||
editor.create_model(BookForeignObj)
|
editor.create_model(BookForeignObj)
|
||||||
|
|
Loading…
Reference in New Issue