summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/dialects/sqlite
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqlalchemy/dialects/sqlite')
-rw-r--r--lib/sqlalchemy/dialects/sqlite/base.py67
1 files changed, 39 insertions, 28 deletions
diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py
index f1be0385d..cd462021f 100644
--- a/lib/sqlalchemy/dialects/sqlite/base.py
+++ b/lib/sqlalchemy/dialects/sqlite/base.py
@@ -616,6 +616,8 @@ class SQLiteDialect(default.DefaultDialect):
supports_cast = True
supports_default_values = True
+ _broken_fk_pragma_quotes = False
+
def __init__(self, isolation_level=None, native_datetime=False, **kwargs):
default.DefaultDialect.__init__(self, **kwargs)
self.isolation_level = isolation_level
@@ -635,6 +637,12 @@ class SQLiteDialect(default.DefaultDialect):
self.dbapi.sqlite_version_info >= (3, 7, 11)
# http://www.sqlite.org/releaselog/3_7_11.html
+ # see http://www.sqlalchemy.org/trac/ticket/2568
+ # as well as http://www.sqlite.org/src/info/600482d161
+ self._broken_fk_pragma_quotes = \
+ self.dbapi.sqlite_version_info < (3, 6, 14)
+
+
_isolation_lookup = {
'READ UNCOMMITTED': 1,
'SERIALIZABLE': 0
@@ -851,37 +859,40 @@ class SQLiteDialect(default.DefaultDialect):
if row is None:
break
(numerical_id, rtbl, lcol, rcol) = (row[0], row[2], row[3], row[4])
- # sqlite won't return rcol if the table
- # was created with REFERENCES <tablename>, no col
- if rcol is None:
- rcol = lcol
-
- # see http://www.sqlalchemy.org/trac/ticket/2568
- # as well as http://www.sqlite.org/src/info/600482d161
- if self.dbapi.sqlite_version_info < (3, 6, 14):
- rtbl = re.sub(r'^\"|\"$', '', rtbl)
- try:
- fk = fks[numerical_id]
- except KeyError:
- fk = {
- 'name': None,
- 'constrained_columns': [],
- 'referred_schema': None,
- 'referred_table': rtbl,
- 'referred_columns': []
- }
- fkeys.append(fk)
- fks[numerical_id] = fk
-
- # look up the table based on the given table's engine, not 'self',
- # since it could be a ProxyEngine
- if lcol not in fk['constrained_columns']:
- fk['constrained_columns'].append(lcol)
- if rcol not in fk['referred_columns']:
- fk['referred_columns'].append(rcol)
+ self._parse_fk(fks, fkeys, numerical_id, rtbl, lcol, rcol)
return fkeys
+ def _parse_fk(self, fks, fkeys, numerical_id, rtbl, lcol, rcol):
+ # sqlite won't return rcol if the table
+ # was created with REFERENCES <tablename>, no col
+ if rcol is None:
+ rcol = lcol
+
+ if self._broken_fk_pragma_quotes:
+ rtbl = re.sub(r'^[\"\[`\']|[\"\]`\']$', '', rtbl)
+
+ try:
+ fk = fks[numerical_id]
+ except KeyError:
+ fk = {
+ 'name': None,
+ 'constrained_columns': [],
+ 'referred_schema': None,
+ 'referred_table': rtbl,
+ 'referred_columns': []
+ }
+ fkeys.append(fk)
+ fks[numerical_id] = fk
+
+ # look up the table based on the given table's engine, not 'self',
+ # since it could be a ProxyEngine
+ if lcol not in fk['constrained_columns']:
+ fk['constrained_columns'].append(lcol)
+ if rcol not in fk['referred_columns']:
+ fk['referred_columns'].append(rcol)
+ return fk
+
@reflection.cache
def get_indexes(self, connection, table_name, schema=None, **kw):
quote = self.identifier_preparer.quote_identifier