Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix unique constraints reflection in SQLite #72

Merged
merged 1 commit into from

2 participants

Roman Podoliaka mike bayer
Roman Podoliaka

Reflection of unique constraints didn't work properly, if reserved
identifiers had been used as column names. In this case column names
would be put in double quotes (e.g. the name of column asc would be
returned as "asc").

This issue is only present in 0.8.4 and not in 0.9.x (looks like this had been fixed in 0.9.x but was not backported to 0.8.x).

The snippet to reproduce:

import sqlalchemy as sa


eng = sa.create_engine('sqlite://')
meta = sa.MetaData(bind=eng)

table = sa.Table(
    'testtbl', meta,
    sa.Column('id', sa.Integer, primary_key=True),
    sa.Column('asc', sa.Integer),
    sa.Column('key', sa.Integer),
    sa.UniqueConstraint('asc', 'key', name='uniq_asc_key')
)
table.create()


inspector = sa.inspect(eng)
print inspector.get_unique_constraints('testtbl')
Roman Podoliaka malor Fix unique constraints reflection in SQLite
Reflection of unique constraints didn't work properly, if reserved
identifiers had been used as column names. In this case column names
would be put in double quotes (e.g. the name of column asc would be
returned as "asc").

This issue is only present in 0.8.4 and not in 0.9.x.
a7e0d3e
mike bayer zzzeek merged commit a7e0d3e into from
mike bayer
Owner

seems like you had sent me that change later and I didn't backport it to 0.8.

Roman Podoliaka

Seems so, sorry :(

Roman Podoliaka malor deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 14, 2014
  1. Roman Podoliaka

    Fix unique constraints reflection in SQLite

    malor authored
    Reflection of unique constraints didn't work properly, if reserved
    identifiers had been used as column names. In this case column names
    would be put in double quotes (e.g. the name of column asc would be
    returned as "asc").
    
    This issue is only present in 0.8.4 and not in 0.9.x.
This page is out of date. Refresh to see the latest.
2  lib/sqlalchemy/dialects/sqlite/base.py
View
@@ -940,7 +940,7 @@ def get_unique_constraints(self, connection, table_name,
UNIQUE_PATTERN = 'CONSTRAINT (\w+) UNIQUE \(([^\)]+)\)'
return [
- {'name': name, 'column_names': [c.strip() for c in cols.split(',')]}
+ {'name': name, 'column_names': [c.strip(' "') for c in cols.split(',')]}
for name, cols in re.findall(UNIQUE_PATTERN, table_data)
]
4 lib/sqlalchemy/testing/suite/test_reflection.py
View
@@ -385,6 +385,7 @@ def _test_get_unique_constraints(self, schema=None):
{'name': 'unique_a_b_c', 'column_names': ['a', 'b', 'c']},
{'name': 'unique_a_c', 'column_names': ['a', 'c']},
{'name': 'unique_b_c', 'column_names': ['b', 'c']},
+ {'name': 'unique_asc_key', 'column_names': ['asc', 'key']},
],
key=operator.itemgetter('name')
)
@@ -394,6 +395,9 @@ def _test_get_unique_constraints(self, schema=None):
Column('a', sa.String(20)),
Column('b', sa.String(30)),
Column('c', sa.Integer),
+ # reserved identifiers
+ Column('asc', sa.String(30)),
+ Column('key', sa.String(30)),
schema=schema
)
for uc in uniques:
Something went wrong with that request. Please try again.