SQLAlchemy具有多个数据库,并且由于重复创建索引而出错

时间:2019-03-21 00:02:29

标签: python postgresql sqlite sqlalchemy

我们有一个包含两个表ModelsDrives的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_fkdrive,然后一切正常。

0 个答案:

没有答案