Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix unique constraints reflection in SQLite #72

Merged
merged 1 commit into from

2 participants

@malor

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')
@malor 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
@zzzeek zzzeek merged commit a7e0d3e into zzzeek:rel_0_8
@zzzeek
Owner

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

@malor

Seems so, sorry :(

@malor malor deleted the malor:fix_sqlite_uc_reflection 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. @malor

    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.
View
2  lib/sqlalchemy/dialects/sqlite/base.py
@@ -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)
]
View
4 lib/sqlalchemy/testing/suite/test_reflection.py
@@ -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.