Alembic:正在运行的创建新表的升级脚本上的“关系不存在”

时间:2019-04-16 16:39:23

标签: python postgresql sqlalchemy alembic

我自动生成了一个新的Alembic迁移脚本,现在alembic upgrade head失败,并出现以下错误(由于我怀疑它是否相关,因此排除了堆栈跟踪的其余部分):

db_migrations_1_c70e60f22f2f | sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "exchange_rate_history" does not exist                                                                        
db_migrations_1_c70e60f22f2f |  [SQL: '\n        ALTER TABLE exchange_rate_history\n        SET (autovacuum_vacuum_scale_factor = 0.1);\n\n        ALTER TABLE exchange_rate_history\n        SET (autovacuum_vacuu
m_threshold = 5000);\n\n        ALTER TABLE exchange_rate_history\n        SET (autovacuum_analyze_scale_factor = 0.1);\n\n        ALTER TABLE exchange_rate_history\n        SET (autovacuum_analyze_threshold = 5
000);\n        '] (Background on this error at: http://sqlalche.me/e/f405)  

(从技术上讲,这是在Docker容器内部,该容器在与它联网的另一个Docker容器中运行的PostgreSQL DB上执行alembic.command.upgrade(alembic_cfg, revision="head")

据我所知,脚本和模型都非常简单。

型号:

@generic_repr
class ExchangeRateHistory(Base):
    __tablename__ = 'exchange_rate_history'

    id = Column(Integer, autoincrement=True, primary_key=True)
    base_currency = Column(Enum(Currencies), nullable=False)
    target_currency = Column(Enum(Currencies), nullable=False)
    exchange_rate = Column(Float, nullable=False)
    date = Column(Date, nullable=False, index=True)

脚本:

"""Add exchange rate history table

Revision ID: 053
Revises: 052
Create Date: 2019-04-16 12:11:43.994327

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql


# revision identifiers, used by Alembic.
revision = '053'
down_revision = '052'
branch_labels = None
depends_on = None

currencies = postgresql.ENUM(
    'USD', 'GBP', 'CNY', 'BRL', name='currencies', create_type=False
)

def upgrade():
    op.create_table(
        'exchange_rate_history',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('base_currency', currencies, nullable=False),
        sa.Column('target_currency', currencies, nullable=False),
        sa.Column('exchange_rate', sa.Float(), nullable=False),
        sa.Column('date', sa.Date(), nullable=False),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_index(
        op.f('ix_exchange_rate_history_date'),
        'exchange_rate_history',
        ['date'],
        unique=False,
    )

def downgrade():
    op.drop_index(
        op.f('ix_exchange_rate_history_date'),
        table_name='exchange_rate_history',
    )
    op.drop_table('exchange_rate_history')

这可能是一个相当具体的问题,但是老实说,我对此感到很困惑,非常感谢您提供任何指导。谢谢!

0 个答案:

没有答案