我的动画表模型如下:数据库中没有测试列,我想用alembic更新它。
class Animation(Base):
__tablename__="Animation"
id=Column(Integer,primary_key=True,index=True,autoincrement=True)
name=Column(VARCHAR(100))
description=Column(VARCHAR(100))
animation=Column(VARCHAR(100))
created_at=Column(DateTime)
updated_at=Column(DateTime)
test=Column(DateTime)但是alembic检测到了所有现存的表并试图重新创建它们。因此,当.It运行“”命令时,ı会返回一个错误,该命令表示表已经存在。
当ı运行自动生成命令时,版本脚本如下:
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('Animation',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.VARCHAR(length=100), nullable=True),
sa.Column('description', sa.VARCHAR(length=100), nullable=True),
sa.Column('animation', sa.VARCHAR(length=100), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.Column('test', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_Animation_id'), 'Animation', ['id'], unique=False)
op.create_table('Faq',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('title', sa.VARCHAR(length=100), nullable=True),
sa.Column('description', sa.VARCHAR(length=100), nullable=True),
sa.Column('animation_id', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_Faq_id'), 'Faq', ['id'], unique=False)
op.create_table('Service',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('plate', sa.VARCHAR(length=100), nullable=True),
sa.Column('arrive_city', sa.VARCHAR(length=100), nullable=True),
sa.Column('arrive_district', sa.VARCHAR(length=100), nullable=True),
sa.Column('departure_time', sa.Time(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_Service_id'), 'Service', ['id'], unique=False)
op.drop_index('ix_Service_id', table_name='service')
op.drop_table('service')
op.drop_index('ix_Animation_id', table_name='animation')
op.drop_table('animation')
op.drop_index('ix_Faq_id', table_name='faq')
op.drop_table('faq')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('faq',
sa.Column('id', mysql.INTEGER(display_width=11), autoincrement=True, nullable=False),
sa.Column('title', mysql.VARCHAR(collation='utf8_turkish_ci', length=100), nullable=True),
sa.Column('description', mysql.VARCHAR(collation='utf8_turkish_ci', length=100), nullable=True),
sa.Column('animation_id', mysql.INTEGER(display_width=11), autoincrement=False, nullable=True),
sa.Column('created_at', mysql.DATETIME(), nullable=True),
sa.Column('updated_at', mysql.DATETIME(), nullable=True),
sa.PrimaryKeyConstraint('id'),
mysql_collate='utf8_turkish_ci',
mysql_default_charset='utf8',
mysql_engine='MyISAM'
)
op.create_index('ix_Faq_id', 'faq', ['id'], unique=False)
op.create_table('animation',
sa.Column('id', mysql.INTEGER(display_width=11), autoincrement=True, nullable=False),
sa.Column('name', mysql.VARCHAR(collation='utf8_turkish_ci', length=100), nullable=True),
sa.Column('description', mysql.VARCHAR(collation='utf8_turkish_ci', length=100), nullable=True),
sa.Column('animation', mysql.VARCHAR(collation='utf8_turkish_ci', length=100), nullable=True),
sa.Column('created_at', mysql.DATETIME(), nullable=True),
sa.Column('updated_at', mysql.DATETIME(), nullable=True),
sa.PrimaryKeyConstraint('id'),
mysql_collate='utf8_turkish_ci',
mysql_default_charset='utf8',
mysql_engine='MyISAM'
)
op.create_index('ix_Animation_id', 'animation', ['id'], unique=False)
op.create_table('service',
sa.Column('id', mysql.INTEGER(display_width=11), autoincrement=True, nullable=False),
sa.Column('plate', mysql.VARCHAR(collation='utf8_turkish_ci', length=100), nullable=True),
sa.Column('arrive_city', mysql.VARCHAR(collation='utf8_turkish_ci', length=100), nullable=True),
sa.Column('arrive_district', mysql.VARCHAR(collation='utf8_turkish_ci', length=100), nullable=True),
sa.Column('departure_time', mysql.TIME(), nullable=True),
sa.Column('created_at', mysql.DATETIME(), nullable=True),
sa.Column('updated_at', mysql.DATETIME(), nullable=True),
sa.PrimaryKeyConstraint('id'),
mysql_collate='utf8_turkish_ci',
mysql_default_charset='utf8',
mysql_engine='MyISAM'
)
op.create_index('ix_Service_id', 'service', ['id'], unique=False)
op.drop_index(op.f('ix_Service_id'), table_name='Service')
op.drop_table('Service')
op.drop_index(op.f('ix_Faq_id'), table_name='Faq')
op.drop_table('Faq')
op.drop_index(op.f('ix_Animation_id'), table_name='Animation')
op.drop_table('Animation')
# ### end Alembic commands ###我是新来的,我找不到任何解决办法。
发布于 2021-09-10 12:35:38
实际的问题是:
class Animation(Base):
__tablename__="Animation"
id=Column(Integer,primary_key=True,index=True,autoincrement=True)
name=Column(VARCHAR(100))
description=Column(VARCHAR(100))
animation=Column(VARCHAR(100))
created_at=Column(DateTime)
updated_at=Column(DateTime)
test=Column(DateTime)表名必须以小写字母开头。Alembic试图找到一个以大写A开头的表,但在MySQL中,表名以小写字母开头。所以它们不匹配。所以阿伦比克试图重新创造它们。这解决了我的问题。
发布于 2021-08-25 09:48:28
听起来,您已经在数据库中创建了表,并且现在只开始使用alembic,因为您需要添加一个新的列。所以alembic假设它需要创建所有的表。
要解决这个问题,您可以:
column
DELETE FROM alembic_version,如果表存在
H 212H 113执行新column
generate的代码,并按照通常的H 219F 220运行一个典型的迁移。alembic stamp在数据库中设置以前迁移的引用,因此alembic知道从哪里开始。通过生成初始迁移并对其进行stamp,我们让alembic知道它只需要运行新列的迁移。
您可以简单地运行alembic stamp head,而不是生成和加盖初始迁移,但我还没有测试这个。
https://stackoverflow.com/questions/68918954
复制相似问题