This is the SQL I want to generate:
CREATE UNIQUE INDEX users_lower_email_key ON users (LOWER(email));
From the SQLAlchemy Index documentation I would expect this to work:
Index('users_lower_email_key', func.lower(users.c.email), unique=True)
But after I call metadata.create(engine) the table is created but this index is not. I've tried:
from conf import dsn, DEBUG
engine = create_engine(dsn.engine_info())
metadata = MetaData()
metadata.bind = engine
users = Table('users', metadata,
Column('user_id', Integer, primary_key=True),
Column('email', String),
Column('first_name', String, nullable=False),
Column('last_name', String, nullable=False),
)
Index('users_lower_email_key', func.lower(users.c.email), unique=True)
metadata.create_all(engine)
Viewing the table definition in PostgreSQL I see that this index was not created.
\d users
Table "public.users"
Column | Type | Modifiers
------------+-------------------+---------------------------------------------------------
user_id | integer | not null default nextval('users_user_id_seq'::regclass)
email | character varying |
first_name | character varying | not null
last_name | character varying | not null
Indexes:
"users_pkey" PRIMARY KEY, btree (user_id)
How can I create my lower, unique index?
INTEGERcolumn.Stringtype, then the example works perfectly and without a hassle.