summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql/introspection.py
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2019-07-20 15:15:41 +0200
committerGitHub <noreply@github.com>2019-07-20 15:15:41 +0200
commit842fd620ff42815dd7741a60e09005af11740395 (patch)
tree2d994619f24d5daef09cf5e95d2a0cc1f10beda1 /django/db/backends/postgresql/introspection.py
parent9f11939dd1477981da2f3a5487d80101faa21230 (diff)
downloaddjango-842fd620ff42815dd7741a60e09005af11740395.tar.gz
Simplified get_key_columns()/get_relations() introspection methods for PostgreSQL.
Diffstat (limited to 'django/db/backends/postgresql/introspection.py')
-rw-r--r--django/db/backends/postgresql/introspection.py21
1 files changed, 4 insertions, 17 deletions
diff --git a/django/db/backends/postgresql/introspection.py b/django/db/backends/postgresql/introspection.py
index 9bd75bd847..dc3394dbe7 100644
--- a/django/db/backends/postgresql/introspection.py
+++ b/django/db/backends/postgresql/introspection.py
@@ -115,8 +115,11 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
Return a dictionary of {field_name: (field_name_other_table, other_table)}
representing all relationships to the given table.
"""
+ return {row[0]: (row[2], row[1]) for row in self.get_key_columns(cursor, table_name)}
+
+ def get_key_columns(self, cursor, table_name):
cursor.execute("""
- SELECT c2.relname, a1.attname, a2.attname
+ SELECT a1.attname, c2.relname, a2.attname
FROM pg_constraint con
LEFT JOIN pg_class c1 ON con.conrelid = c1.oid
LEFT JOIN pg_class c2 ON con.confrelid = c2.oid
@@ -124,22 +127,6 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
LEFT JOIN pg_attribute a2 ON c2.oid = a2.attrelid AND a2.attnum = con.confkey[1]
WHERE c1.relname = %s AND con.contype = 'f'
""", [table_name])
- return {row[1]: (row[2], row[0]) for row in cursor.fetchall()}
-
- def get_key_columns(self, cursor, table_name):
- cursor.execute("""
- SELECT kcu.column_name, ccu.table_name AS referenced_table, ccu.column_name AS referenced_column
- FROM information_schema.constraint_column_usage ccu
- LEFT JOIN information_schema.key_column_usage kcu
- ON ccu.constraint_catalog = kcu.constraint_catalog
- AND ccu.constraint_schema = kcu.constraint_schema
- AND ccu.constraint_name = kcu.constraint_name
- LEFT JOIN information_schema.table_constraints tc
- ON ccu.constraint_catalog = tc.constraint_catalog
- AND ccu.constraint_schema = tc.constraint_schema
- AND ccu.constraint_name = tc.constraint_name
- WHERE kcu.table_name = %s AND tc.constraint_type = 'FOREIGN KEY'
- """, [table_name])
return cursor.fetchall()
def get_constraints(self, cursor, table_name):