Fixed #27372 -- Fixed introspection of SQLite foreign keys with spaces in DDL.

Thanks samuller for the report and initial patch.
This commit is contained in:
Saulius Žemaitaitis 2016-11-05 13:46:59 +01:00 committed by Tim Graham
parent 09da1e79de
commit f28d29e8b7
2 changed files with 14 additions and 12 deletions

View File

@ -132,7 +132,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
if field_desc.startswith("FOREIGN KEY"): if field_desc.startswith("FOREIGN KEY"):
# Find name of the target FK field # Find name of the target FK field
m = re.match(r'FOREIGN KEY\(([^\)]*)\).*', field_desc, re.I) m = re.match(r'FOREIGN KEY\s*\(([^\)]*)\).*', field_desc, re.I)
field_name = m.groups()[0].strip('"') field_name = m.groups()[0].strip('"')
else: else:
field_name = field_desc.split()[0].strip('"') field_name = field_desc.split()[0].strip('"')

View File

@ -143,17 +143,19 @@ class IntrospectionTests(TransactionTestCase):
@skipUnless(connection.vendor == 'sqlite', "This is an sqlite-specific issue") @skipUnless(connection.vendor == 'sqlite', "This is an sqlite-specific issue")
def test_get_relations_alt_format(self): def test_get_relations_alt_format(self):
"""With SQLite, foreign keys can be added with different syntaxes.""" """
with connection.cursor() as cursor: With SQLite, foreign keys can be added with different syntaxes and
cursor.fetchone = mock.Mock( formatting.
return_value=[ """
"CREATE TABLE track(id, art_id INTEGER, FOREIGN KEY(art_id) REFERENCES {}(id));".format( create_table_statements = [
Article._meta.db_table "CREATE TABLE track(id, art_id INTEGER, FOREIGN KEY(art_id) REFERENCES {}(id));",
) "CREATE TABLE track(id, art_id INTEGER, FOREIGN KEY (art_id) REFERENCES {}(id));"
] ]
) for statement in create_table_statements:
relations = connection.introspection.get_relations(cursor, 'mocked_table') with connection.cursor() as cursor:
self.assertEqual(relations, {'art_id': ('id', Article._meta.db_table)}) cursor.fetchone = mock.Mock(return_value=[statement.format(Article._meta.db_table)])
relations = connection.introspection.get_relations(cursor, 'mocked_table')
self.assertEqual(relations, {'art_id': ('id', Article._meta.db_table)})
@skipUnlessDBFeature('can_introspect_foreign_keys') @skipUnlessDBFeature('can_introspect_foreign_keys')
def test_get_key_columns(self): def test_get_key_columns(self):