Refs -- Moved SQLite table constraint parsing to a method.
@@ -217,23 +217,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
}
return constraints
- def get_constraints(self, cursor, table_name):
- """
- Retrieve any constraints or keys (unique, pk, fk, check, index) across
- one or more columns.
- """
- constraints = {}
- # Find inline check constraints.
- try:
- table_schema = cursor.execute(
- "SELECT sql FROM sqlite_master WHERE type='table' and name=%s" % (
- self.connection.ops.quote_name(table_name),
- )
- ).fetchone()[0]
- except TypeError:
- # table_name is a view.
- pass
- else:
+ def _parse_table_constraints(self, sql):
# Check constraint parsing is based of SQLite syntax diagram.
# https://www.sqlite.org/syntaxdiagrams.html#table-constraint
def next_ttype(ttype):
@@ -241,7 +225,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
if token.ttype == ttype:
return token
- statement = sqlparse.parse(table_schema)[0]
+ statement = sqlparse.parse(sql)[0]
+ constraints = {}
tokens = statement.flatten()
for token in tokens:
name = None
@@ -276,6 +261,27 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
'foreign_key': None,
'index': False,
}
+ return constraints
+
+ def get_constraints(self, cursor, table_name):
+ """
+ Retrieve any constraints or keys (unique, pk, fk, check, index) across
+ one or more columns.
+ """
+ constraints = {}
+ # Find inline check constraints.
+ try:
+ table_schema = cursor.execute(
+ "SELECT sql FROM sqlite_master WHERE type='table' and name=%s" % (
+ self.connection.ops.quote_name(table_name),
+ )
+ ).fetchone()[0]
+ except TypeError:
+ # table_name is a view.
+ pass
+ else:
+ constraints.update(self._parse_table_constraints(table_schema))
+
# Get the index info
cursor.execute("PRAGMA index_list(%s)" % self.connection.ops.quote_name(table_name))
for row in cursor.fetchall():
