diff --git a/django/db/backends/sqlite3/introspection.py b/django/db/backends/sqlite3/introspection.py index bcf11e1751..7c5c159b64 100644 --- a/django/db/backends/sqlite3/introspection.py +++ b/django/db/backends/sqlite3/introspection.py @@ -1,4 +1,3 @@ -import re from collections import namedtuple import sqlparse @@ -117,61 +116,16 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): def get_relations(self, cursor, table_name): """ - Return a dictionary of {field_name: (field_name_other_table, other_table)} + Return a dictionary of {column_name: (ref_column_name, ref_table_name)} representing all foreign keys in the given table. """ - # Dictionary of relations to return - relations = {} - - # Schema for this table cursor.execute( - "SELECT sql, type FROM sqlite_master " - "WHERE tbl_name = %s AND type IN ('table', 'view')", - [table_name] + 'PRAGMA foreign_key_list(%s)' % self.connection.ops.quote_name(table_name) ) - create_sql, table_type = cursor.fetchone() - if table_type == 'view': - # It might be a view, then no results will be returned - return relations - results = create_sql[create_sql.index('(') + 1:create_sql.rindex(')')] - - # Walk through and look for references to other tables. SQLite doesn't - # really have enforced references, but since it echoes out the SQL used - # to create the table we can look for REFERENCES statements used there. - for field_desc in results.split(','): - field_desc = field_desc.strip() - if field_desc.startswith("UNIQUE"): - continue - - m = re.search(r'references (\S*) ?\(["|]?(.*)["|]?\)', field_desc, re.I) - if not m: - continue - table, column = [s.strip('"') for s in m.groups()] - - if field_desc.startswith("FOREIGN KEY"): - # Find name of the target FK field - m = re.match(r'FOREIGN KEY\s*\(([^\)]*)\).*', field_desc, re.I) - field_name = m[1].strip('"') - else: - field_name = field_desc.split()[0].strip('"') - - cursor.execute("SELECT sql FROM sqlite_master WHERE tbl_name = %s", [table]) - result = cursor.fetchall()[0] - other_table_results = result[0].strip() - li, ri = other_table_results.index('('), other_table_results.rindex(')') - other_table_results = other_table_results[li + 1:ri] - - for other_desc in other_table_results.split(','): - other_desc = other_desc.strip() - if other_desc.startswith('UNIQUE'): - continue - - other_name = other_desc.split(' ', 1)[0].strip('"') - if other_name == column: - relations[field_name] = (other_name, table) - break - - return relations + return { + column_name: (ref_column_name, ref_table_name) + for _, _, ref_table_name, column_name, ref_column_name, *_ in cursor.fetchall() + } def get_primary_key_column(self, cursor, table_name): """Return the column name of the primary key for the given table.""" diff --git a/tests/introspection/tests.py b/tests/introspection/tests.py index b55714f93e..d138e71bd6 100644 --- a/tests/introspection/tests.py +++ b/tests/introspection/tests.py @@ -1,5 +1,3 @@ -from unittest import mock, skipUnless - from django.db import DatabaseError, connection from django.db.models import Index from django.test import TransactionTestCase, skipUnlessDBFeature @@ -152,22 +150,6 @@ class IntrospectionTests(TransactionTestCase): editor.add_field(Article, body) self.assertEqual(relations, expected_relations) - @skipUnless(connection.vendor == 'sqlite', "This is an sqlite-specific issue") - def test_get_relations_alt_format(self): - """ - With SQLite, foreign keys can be added with different syntaxes and - formatting. - """ - create_table_statements = [ - "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: - with connection.cursor() as cursor: - cursor.fetchone = mock.Mock(return_value=[statement.format(Article._meta.db_table), 'table']) - relations = connection.introspection.get_relations(cursor, 'mocked_table') - self.assertEqual(relations, {'art_id': ('id', Article._meta.db_table)}) - def test_get_primary_key_column(self): with connection.cursor() as cursor: primary_key_column = connection.introspection.get_primary_key_column(cursor, Article._meta.db_table)