I want to make a case-insensitive unique constraint in SQLAlchemy, which would work with both Postgres and SQLite.
In Postgres it is achieved:
- SQL:
CREATE UNIQUE INDEX my_index ON my_table (lower(my_field));
- SQLAlchemy:
Index('my_index', func.lower(my_field), unique=True)
In SQLite it is achieved using:
- SQL:
CREATE UNIQUE INDEX my_index ON my_table (my_field COLLATE NOCASE);
- SQLAlchemy:
Index('my_index', collate(my_field, 'nocase'), unique=True)
So I'm trying to subclass the FunctionElement
in order to create my own function-like construct, which would compile either to my_field COLLATE NOCASE
or lower(my_field)
depending on DBMS. I tried to follow the guide to compilation extension, but I'm getting an error (see traceback below), which I can follow through SQLAlchemy source code, but which I cannot understand. I tried subclassing other things, such as ColumnElement
, ColumnClause
, ClauseElement
, but I'm getting similar errors.
Here is the code that reproduces the error. I used version 0.8.2 of SQLAlchemy.
from sqlalchemy import create_engine, Column, String, Integer
from sqlalchemy.types import TypeDecorator, VARCHAR
from sqlalchemy import func, Index, collate
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.expression import (
# Not sure which one to subclass
FunctionElement, ColumnElement, ColumnClause, ClauseElement
)
engine = create_engine('sqlite:///:memory:', echo=True)
Base = declarative_base()
class CaseInsensitive(FunctionElement):
name = 'CaseInsensitive'
type = VARCHAR()
@compiles(CaseInsensitive, 'sqlite')
def case_insensitive_sqlite(element, compiler, **kw):
arg1, = list(element.clauses)
return collate(compiler.process(arg1), 'nocase')
@compiles(CaseInsensitive, 'postgresql')
def case_insensitive_postgresql(element, compiler, **kw):
arg1, = list(element.clauses)
return func.lower(compiler.process(arg1))
class MyTable(Base):
__tablename__ = 'my_table'
id = Column(Integer, primary_key=True)
my_field = Column(String)
__table_args__ = (
Index('idx', CaseInsensitive(my_field), unique=True),
)
Traceback (most recent call last):
File "tmp.py", line 33, in <module>
class MyTable(Base):
File "/Users/vladimir/git/wb/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py", line 50, in __init__
_as_declarative(cls, classname, cls.__dict__)
File "/Users/vladimir/git/wb/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 222, in _as_declarative
**table_kw)
File "/Users/vladimir/git/wb/lib/python2.7/site-packages/sqlalchemy/schema.py", line 332, in __new__
table._init(name, metadata, *args, **kw)
File "/Users/vladimir/git/wb/lib/python2.7/site-packages/sqlalchemy/schema.py", line 400, in _init
self._init_items(*args)
File "/Users/vladimir/git/wb/lib/python2.7/site-packages/sqlalchemy/schema.py", line 65, in _init_items
item._set_parent_with_dispatch(self)
File "/Users/vladimir/git/wb/lib/python2.7/site-packages/sqlalchemy/events.py", line 236, in _set_parent_with_dispatch
self._set_parent(parent)
File "/Users/vladimir/git/wb/lib/python2.7/site-packages/sqlalchemy/schema.py", line 2421, in _set_parent
ColumnCollectionMixin._set_parent(self, table)
File "/Users/vladimir/git/wb/lib/python2.7/site-packages/sqlalchemy/schema.py", line 2035, in _set_parent
self.columns.add(col)
File "/Users/vladimir/git/wb/lib/python2.7/site-packages/sqlalchemy/sql/expression.py", line 2477, in add
self[column.key] = column
File "/Users/vladimir/git/wb/lib/python2.7/site-packages/sqlalchemy/sql/expression.py", line 2296, in __getattr__
key)
AttributeError: Neither 'CaseInsensitive' object nor 'Comparator' object has an attribute 'key'