diff options
| author | Johannes Erdfelt <johannes@erdfelt.com> | 2014-09-10 07:37:59 -0700 |
|---|---|---|
| committer | Johannes Erdfelt <johannes@erdfelt.com> | 2014-09-17 13:19:50 -0700 |
| commit | 7fa21b22989f6d53ff70a8df71fc6d210c556e07 (patch) | |
| tree | a6e8f1ee74213340de60d5852f8d10ad56bc212b /test/dialect/mysql | |
| parent | 1f2f88d8ffaac5ae98de097e548e205778686cd5 (diff) | |
| download | sqlalchemy-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 'test/dialect/mysql')
| -rw-r--r-- | test/dialect/mysql/test_reflection.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/test/dialect/mysql/test_reflection.py b/test/dialect/mysql/test_reflection.py index bf35a2c6b..b8f72b942 100644 --- a/test/dialect/mysql/test_reflection.py +++ b/test/dialect/mysql/test_reflection.py @@ -283,6 +283,37 @@ class ReflectionTest(fixtures.TestBase, AssertsExecutionResults): view_names = dialect.get_view_names(connection, "information_schema") self.assert_('TABLES' in view_names) + def test_reflection_with_unique_constraint(self): + insp = inspect(testing.db) + + uc_table = Table('mysql_uc', MetaData(testing.db), + Column('a', String(10)), + UniqueConstraint('a', name='uc_a')) + + try: + uc_table.create() + + # MySQL converts unique constraints into unique indexes and + # the 0.9 API returns it as both an index and a constraint + indexes = set(i['name'] for i in insp.get_indexes('mysql_uc')) + constraints = set(i['name'] + for i in insp.get_unique_constraints('mysql_uc')) + + self.assert_('uc_a' in indexes) + self.assert_('uc_a' in constraints) + + # However, upon creating a Table object via reflection, it should + # only appear as a unique index and not a constraint + reflected = Table('mysql_uc', MetaData(testing.db), autoload=True) + + indexes = set(i.name for i in reflected.indexes) + constraints = set(uc.name for uc in reflected.constraints) + + self.assert_('uc_a' in indexes) + self.assert_('uc_a' not in constraints) + finally: + uc_table.drop() + class RawReflectionTest(fixtures.TestBase): def setup(self): |
