我是如此alembic的新手,所以我可能会错过它概念上的一点,但问题是。
我在烧瓶应用程序中有一些sqlalchemy表,如下所示:
class Data(Base):
__tablename__ = 'Data'
__table_args__ = {'schema': 'schema'}
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
Base = declarative_base()
engine = create_engine(db_link, pool_size=100, max_overflow=0)
Base.metadata.create_all(engine)
Session = sessionmaker()
Session.configure(bind=engine)
到那时,我在数据库中手动创建了表,一切都很好。
为了以后在我的项目中取得成效,我希望能够使用alembic迁移我的数据库。因为我将使用的一些表(在不同的模式中)是只读的,并且是由另一个程序创建的,所以我只想迁移一些sqlalchemy表。因此我的升级脚本看起来像(由alembic revision--autogenerate创建):
revision = 'bb1d39b7eee1'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('Data',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('id'),
schema='schema'
)
...
当我现在使用空数据库将架构迁移到时:
alembic upgrade head
sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42S01', "[42S01] [M
icrosoft][SQL Server Native Client 11.0][SQL Server]There is already an object n
amed 'Data' in the database. (2714) (SQLExecDirectW)") [SQL: '\nCREATE TABLE schema.
[Data] (\n\tid INTEGER NOT NULL IDENTITY(1,1), \n\tname VARCHAR(max) NOT NULL, \
\n\tPRIMARY KEY (id), \n\tCHECK (IN (0, 1))\n)\n\n']
看起来alembic会自动创建所有表,然后再次尝试在我的修订脚本中创建这些表。如果这是真的,我如何告诉alembic不要自动创建任何表,而只运行我创建的脚本?