|
0
|
1 |
from django.db.backends.postgresql.introspection import DatabaseIntrospection as PostgresDatabaseIntrospection |
|
|
2 |
|
|
|
3 |
class DatabaseIntrospection(PostgresDatabaseIntrospection): |
|
|
4 |
|
|
|
5 |
def get_relations(self, cursor, table_name): |
|
|
6 |
""" |
|
|
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. |
|
|
9 |
""" |
|
|
10 |
cursor.execute(""" |
|
|
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 |
|
|
15 |
AND c1.relname = %s |
|
|
16 |
AND con.contype = 'f'""", [table_name]) |
|
|
17 |
relations = {} |
|
|
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]) |
|
|
21 |
return relations |