python - Alembic 降级似乎不理解元数据

标签 python sqlalchemy python-3.5 alembic sqlalchemy-migrate

model.py 如下所示:

import datetime

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Numeric, ForeignKey, DateTime, Boolean
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, relationship

from configs import config_base as config
Base = declarative_base()


class User(Base):
    __tablename__ = 'user'

    id = Column(String, unique=True, primary_key=True)
    name = Column(String(100), nullable=False)
    team_id = Column(String, ForeignKey('team.id'))
    last_modified_on = Column(DateTime, default=datetime.datetime.utcnow())
    team = relationship('Team', back_populates='members')


class Team(Base):
    __tablename__ = 'team'

    id = Column(String, unique=True, primary_key=True)
    name = Column(String, nullable=False)
    bot_access_token = Column(String(100), nullable=False)
    bot_user_id = Column(String(100), nullable=False)
    last_modified_on = Column(DateTime, default=datetime.datetime.utcnow())
    is_active = Column(Boolean, default=True)
    members = relationship('User', back_populates='team')
    is_first_time_news = Column(Boolean, default=True)

engine = create_engine(config.SQLALCHEMY_DATABASE_URI)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)

我刚刚通过这个 alembic 迁移添加了 is_first_time_news:

revision = '6f9e2d360276'
down_revision = None
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa


def upgrade():
    op.add_column('team', sa.Column('is_first_time_news', sa.Boolean, default=False))


def downgrade():
    op.drop_column('team', sa.Column('is_first_time_news', sa.Boolean))

alembic upgrade head 效果很好。

但是当我执行 alembic downgrade -1 时,我得到了一个奇怪的异常:

AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute '_columns'

最佳答案

你在使用 sqlite 吗? Sqlite 不允许您从 方案。当我尝试降级正在测试的本地 sqlite 数据库时,我遇到了类似的问题。

SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table.

https://www.sqlite.org/lang_altertable.html

关于python - Alembic 降级似乎不理解元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37490575/

相关文章:

python - 定义变量类型! PYTHON

python - Pyinstaller:创建具有多个依赖项的可执行文件 - "TypeError"

python - 使用 SQLAlchemy 添加多对多关系

python - Pylons 1.0 AttributeError : 'module' object has no attribute 'metadata'

Python 在第一个字符后中断字符串

python - 生成连接范围的 1D​​ NumPy 数组

python - 全类异常(exception)

python - 按字典的值排序,如果值相等则按键排序,然后输出列表

python - Python 中的预计算对象

selenium - 使用 Python 和 Selenium 将照片上传到 Craigslist