Alembic降级似乎并不理解元数据

时间:2016-05-27 18:58:45

标签: 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时,我得到一个奇怪的例外:

  

属性错误:没有'列'对象也不是'比较器'对象有一个   属性' _columns'

2 个答案:

答案 0 :(得分:0)

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

  

SQLite支持ALTER TABLE的有限子集。 ALTER TABLE   SQLite中的命令允许用户重命名表或添加新表   列到现有表。

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

答案 1 :(得分:0)

尝试:

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