summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/dialects/postgresql/base.py
diff options
context:
space:
mode:
authorJohannes Erdfelt <johannes@erdfelt.com>2014-09-10 07:37:59 -0700
committerJohannes Erdfelt <johannes@erdfelt.com>2014-09-17 13:19:50 -0700
commit7fa21b22989f6d53ff70a8df71fc6d210c556e07 (patch)
treea6e8f1ee74213340de60d5852f8d10ad56bc212b /lib/sqlalchemy/dialects/postgresql/base.py
parent1f2f88d8ffaac5ae98de097e548e205778686cd5 (diff)
downloadsqlalchemy-7fa21b22989f6d53ff70a8df71fc6d210c556e07.tar.gz
Reflect unique constraints when reflecting a Table object
Calls to reflect a table did not create any UniqueConstraint objects. The reflection core made no calls to get_unique_constraints and as a result, the sqlite dialect would never reflect any unique constraints. MySQL transparently converts unique constraints into unique indexes, but SQLAlchemy would reflect those as an Index object and as a UniqueConstraint. The reflection core will now deduplicate the unique constraints. PostgreSQL would reflect unique constraints as an Index object and as a UniqueConstraint object. The reflection core will now deduplicate the unique indexes.
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/base.py')
-rw-r--r--lib/sqlalchemy/dialects/postgresql/base.py18
1 files changed, 13 insertions, 5 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py
index b9a0d461b..556493b3c 100644
--- a/lib/sqlalchemy/dialects/postgresql/base.py
+++ b/lib/sqlalchemy/dialects/postgresql/base.py
@@ -2471,14 +2471,19 @@ class PGDialect(default.DefaultDialect):
SELECT
i.relname as relname,
ix.indisunique, ix.indexprs, ix.indpred,
- a.attname, a.attnum, ix.indkey%s
+ a.attname, a.attnum, c.conrelid, ix.indkey%s
FROM
pg_class t
join pg_index ix on t.oid = ix.indrelid
- join pg_class i on i.oid=ix.indexrelid
+ join pg_class i on i.oid = ix.indexrelid
left outer join
pg_attribute a
- on t.oid=a.attrelid and %s
+ on t.oid = a.attrelid and %s
+ left outer join
+ pg_constraint c
+ on (ix.indrelid = c.conrelid and
+ ix.indexrelid = c.conindid and
+ c.contype in ('p', 'u', 'x'))
WHERE
t.relkind IN ('r', 'v', 'f', 'm')
and t.oid = :table_oid
@@ -2501,7 +2506,7 @@ class PGDialect(default.DefaultDialect):
sv_idx_name = None
for row in c.fetchall():
- idx_name, unique, expr, prd, col, col_num, idx_key = row
+ idx_name, unique, expr, prd, col, col_num, conrelid, idx_key = row
if expr:
if idx_name != sv_idx_name:
@@ -2523,11 +2528,14 @@ class PGDialect(default.DefaultDialect):
index['cols'][col_num] = col
index['key'] = [int(k.strip()) for k in idx_key.split()]
index['unique'] = unique
+ index['duplicates_constraint'] = (None if conrelid is None
+ else idx_name)
return [
{'name': name,
'unique': idx['unique'],
- 'column_names': [idx['cols'][i] for i in idx['key']]}
+ 'column_names': [idx['cols'][i] for i in idx['key']],
+ 'duplicates_constraint': idx['duplicates_constraint']}
for name, idx in indexes.items()
]