我们有一个包含两个表Models
和Drives
的postgres数据库,我们使用sqlalchemy创建查询来分析这些表中的数据。
Models
表具有以下架构:
CREATE TABLE models (
id SERIAL PRIMARY KEY,
vendor_name character varying(32) NOT NULL,
model character varying(32) NOT NULL,
drive_capacity bigint NOT NULL
);
-- Indices -------------------------------------------------------
CREATE UNIQUE INDEX models_pkey ON models(id int4_ops);
CREATE INDEX idx_models_vendor_name ON models(vendor_name text_ops);
CREATE INDEX idx_models_model ON models(model text_ops);
,并由Models
中的tables.py
类表示:
class Models(Base, DeferredReflection):
__tablename__ = "models"
id = Column("id", Integer, primary_key=True)
Drives
表具有以下架构:
CREATE TABLE drives (
id SERIAL PRIMARY KEY,
serial_number character varying(32) NOT NULL UNIQUE,
model integer NOT NULL REFERENCES models(id),
role character varying(16) NOT NULL
);
-- Indices -------------------------------------------------------
CREATE UNIQUE INDEX drives_pkey ON drives(id int4_ops);
CREATE UNIQUE INDEX drives_serial_number_key ON drives(serial_number text_ops);
CREATE INDEX drives_role_idx ON drives(role text_ops);
CREATE INDEX index_drives_model_fk ON drives(model int4_ops);
,并在Drives
中用tables.py
类表示:
class Drives(Base, DeferredReflection):
__tablename__ = "drives"
id = Column("id", Integer, primary_key=True)
model = Column("model", Integer, ForeignKey(Models.id), nullable=False)
您会注意到,两个表都在特定列上创建了其他索引。在我们的单元测试基础结构中,我们创建一个内存中的sqlite数据库,并创建具有与postgresql数据库相同模式的表和索引。然后,我们将伪造的行插入sqlite实例,在sqlite数据库之上运行真实查询,并检查真实查询的正确性。代码如下:
sqlite_engine = create_engine('sqlite:///', echo=True)
from tables import Base
Base.metadata.create_all(bind=sqlite_engine)
# add fake rows to sqlite
# run real query on sqlite engine
# compare against expected output to confirm correctness of real queries.
但是,我在上面的create_all
步骤中遇到了错误。虽然我可以创建模型表及其索引,但是我得到drives
表的索引已存在错误:
cursor = <sqlite3.Cursor object at 0x7f466b877570>
statement = 'CREATE INDEX index_drives_model_fk ON drives (model)'
parameters = ()
context = <sqlalchemy.dialects.sqlite.base.SQLiteExecutionContext object at 0x7f466b2f4160>
def do_execute(self, cursor, statement, parameters, context=None):
> cursor.execute(statement, parameters)
E sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) index index_drives_model_fk already exists [SQL: 'CREATE INDEX index_drives_model_fk ON drives (model)'] (Background on this error at: http://sqlalche.me/e/e3q8)
对于echo=True
,我注意到SQLAlchemy发出以下语句:
CREATE TABLE models (
id INTEGER NOT NULL,
vendor_name VARCHAR(32) NOT NULL,
model VARCHAR(32) NOT NULL,
drive_capacity BIGINT NOT NULL,
CONSTRAINT models_pkey PRIMARY KEY (id)
)
log.py 110 INFO COMMIT
log.py 110 INFO CREATE INDEX idx_models_model ON models (model)
log.py 110 INFO COMMIT
log.py 110 INFO CREATE INDEX idx_models_vendor_name ON models (vendor_name)
log.py 110 INFO COMMIT
log.py 110 INFO
CREATE TABLE drives (
id INTEGER NOT NULL,
model INTEGER NOT NULL,
serial_number VARCHAR(32) NOT NULL,
role VARCHAR(16) NOT NULL,
CONSTRAINT drives_pkey PRIMARY KEY (id),
FOREIGN KEY(model) REFERENCES models (id),
CONSTRAINT drives_serial_number_key UNIQUE (serial_number),
CONSTRAINT drives_serial_number_key UNIQUE (serial_number)
)
log.py 110 INFO COMMIT
log.py 110 INFO CREATE INDEX index_drives_model_fk ON drives (model)
log.py 110 INFO COMMIT
log.py 110 INFO CREATE INDEX drives_role_idx ON drives (role)
log.py 110 INFO COMMIT
log.py 110 INFO CREATE INDEX index_drives_model_fk ON drives (model) <<<<<<---- Why create index_drives_model_fk twice?
log.py 110 INFO ROLLBACK
如果您注意到上面代码段的最后6行,您将看到index_drives_model_fk
被创建了两次,因此出现了错误。我的问题:是什么使sqlalchemy向drives
表发出两个索引创建,但是models
表(以及关联的索引)似乎正确创建了?我还可以确认是否从postgres drives_role_idx
表中删除了index_drives_model_fk
和drive
,然后一切正常。