我正在尝试在file_name
列上创建一个非唯一索引。我期待一个高基数,比如1000行,有950个唯一的文件名。
file_collection = Table ('file_collection', metadata,
Column('id', Integer, primary_key=True),
Column('full_path', String, unique=True, nullable=False),
Column('file_name', String, index=True, nullable=False)
)
我的方言是sqlite。创建表时,不会在file_name
列
CREATE TABLE file_collection (
id INTEGER NOT NULL,
full_path VARCHAR NOT NULL,
file_name VARCHAR NOT NULL,
PRIMARY KEY (id),
UNIQUE (full_path)
)
如何在file_name
列上创建非唯一键?
答案 0 :(得分:3)
请记住,SQLite要求您将非唯一索引创建为单独的语句,而不是像某些RDBM允许的那样在实际的CREATE TABLE
语句中创建。假设我有下表:
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), index=True)
创建将按如下方式运行。
2014-01-03 17:28:48,229 INFO sqlalchemy.engine.base.Engine PRAGMA table_info("users")
2014-01-03 17:28:48,229 INFO sqlalchemy.engine.base.Engine ()
2014-01-03 17:28:48,229 INFO sqlalchemy.engine.base.Engine
CREATE TABLE users (
id INTEGER NOT NULL,
email VARCHAR(255),
PRIMARY KEY (id)
)
2014-01-03 17:28:48,230 INFO sqlalchemy.engine.base.Engine ()
2014-01-03 17:28:48,230 INFO sqlalchemy.engine.base.Engine COMMIT
2014-01-03 17:28:48,230 INFO sqlalchemy.engine.base.Engine CREATE INDEX ix_users_email ON users (email)
2014-01-03 17:28:48,230 INFO sqlalchemy.engine.base.Engine ()
2014-01-03 17:28:48,231 INFO sqlalchemy.engine.base.Engine COMMIT
请注意,CREATE TABLE语句不会显示正在创建的索引,但它会在稍后的后续语句中创建。