这是我要生成的 SQL:
CREATE UNIQUE INDEX users_lower_email_key ON users (LOWER(email));
从 SQLAlchemy Index 文档中,我希望这可以工作:
Index('users_lower_email_key', func.lower(users.c.email), unique=True)
但是在我调用metadata.create(engine)
该表后,该表已创建但该索引未创建。我试过了:
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)
查看 PostgreSQL 中的表定义,我发现该索引没有创建。
\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)
如何创建较低的唯一索引?