1
0
mirror of https://github.com/django/django.git synced 2025-03-14 03:10:45 +00:00

[1.11.x] 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.

Backport of fc48047586a8f92262f55d9d2bfb976325844b23 from master
This commit is contained in:
Mariusz Felisiak 2017-12-05 21:11:20 +01:00
parent 3e52fd7595
commit 1decd0197d
3 changed files with 27 additions and 2 deletions

View File

@ -951,7 +951,7 @@ class BaseDatabaseSchemaEditor(object):
def _create_fk_sql(self, model, field, suffix):
from_table = model._meta.db_table
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
suffix = suffix % {
"to_table": to_table,
@ -962,7 +962,7 @@ class BaseDatabaseSchemaEditor(object):
"table": self.quote_name(from_table),
"name": self.quote_name(self._create_index_name(model, [from_column], suffix=suffix)),
"column": self.quote_name(from_column),
"to_table": self.quote_name(to_table),
"to_table": self.quote_name(field.target_field.model._meta.db_table),
"to_column": self.quote_name(to_column),
"deferrable": self.connection.ops.deferrable_sql(),
}

View File

@ -14,3 +14,6 @@ Bugfixes
* Fixed incorrect class-based model index name generation for models with
quoted ``db_table`` (:ticket:`28876`).
* Fixed incorrect foreign key constraint name for models with quoted
``db_table`` (:ticket:`28876`).

View File

@ -1784,6 +1784,28 @@ class SchemaTests(TransactionTestCase):
with connection.schema_editor() as editor:
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):
with connection.schema_editor() as editor:
editor.create_model(BookForeignObj)