1
from django.db.backends.postgresql.introspection import DatabaseIntrospection as PostgresDatabaseIntrospection
3
class DatabaseIntrospection(PostgresDatabaseIntrospection):
5
def get_relations(self, cursor, table_name):
7
Returns a dictionary of {field_index: (field_index_other_table, other_table)}
8
representing all relationships to the given table. Indexes are 0-based.
11
SELECT con.conkey, con.confkey, c2.relname
12
FROM pg_constraint con, pg_class c1, pg_class c2
13
WHERE c1.oid = con.conrelid
14
AND c2.oid = con.confrelid
16
AND con.contype = 'f'""", [table_name])
18
for row in cursor.fetchall():
19
# row[0] and row[1] are single-item lists, so grab the single item.
20
relations[row[0][0] - 1] = (row[1][0] - 1, row[2])