]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Partial index support with sqlite dialects.
authorKai Groner <kai@gronr.com>
Mon, 26 Jan 2015 19:49:40 +0000 (14:49 -0500)
committerKai Groner <kai@gronr.com>
Mon, 26 Jan 2015 19:50:24 +0000 (14:50 -0500)
From https://www.sqlite.org/partialindex.html
> Partial indexes have been supported in SQLite since version 3.8.0.

Reflection does not expose the predicate of partial indexes.  The
postgresql dialect does detect such indexes and issue a warning.  I
looked into matching this level of support, but the sqlite pragma
index_info does not expose the predicate.  Getting this data would
probably require parsing the CREATE INDEX statement from sqlite_master.

lib/sqlalchemy/dialects/sqlite/base.py

index 1ed89bacb36c89acb63964c1be008a550b5198b3..437a7794acc6141c37d755dac6d6e4a48c3f5943 100644 (file)
@@ -714,9 +714,20 @@ class SQLiteDDLCompiler(compiler.DDLCompiler):
         return preparer.format_table(table, use_schema=False)
 
     def visit_create_index(self, create):
-        return super(SQLiteDDLCompiler, self).visit_create_index(
+        index = create.element
+
+        text = super(SQLiteDDLCompiler, self).visit_create_index(
             create, include_table_schema=False)
 
+        whereclause = index.dialect_options["sqlite"]["where"]
+        if whereclause is not None:
+            where_compiled = self.sql_compiler.process(
+                whereclause, include_table=False,
+                literal_binds=True)
+            text += " WHERE " + where_compiled
+
+        return text
+
 
 class SQLiteTypeCompiler(compiler.GenericTypeCompiler):
     def visit_large_binary(self, type_, **kw):
@@ -823,7 +834,10 @@ class SQLiteDialect(default.DefaultDialect):
     construct_arguments = [
         (sa_schema.Table, {
             "autoincrement": False
-        })
+        }),
+        (sa_schema.Index, {
+            "where": None,
+        }),
     ]
 
     _broken_fk_pragma_quotes = False