sqlalchemy postgresql枚举不会在数据库迁移上创建类型 [英] sqlalchemy postgresql enum does not create type on db migrate

查看:139
本文介绍了sqlalchemy postgresql枚举不会在数据库迁移上创建类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python3下使用Flask开发了一个网络应用.我在db migration/upgrade上的postgresql枚举类型有问题.

I develop a web-app using Flask under Python3. I have a problem with postgresql enum type on db migrate/upgrade.

我在模型中添加了状态"列:

I added a column "status" to model:

class Banner(db.Model):
    ...
    status = db.Column(db.Enum('active', 'inactive', 'archive', name='banner_status'))
    ...

python manage.py db migrate产生的迁移是:

from alembic import op
import sqlalchemy as sa

def upgrade():
    op.add_column('banner', sa.Column('status', sa.Enum('active', 'inactive', 'archive', name='banner_status'), nullable=True))

def downgrade():
    op.drop_column('banner', 'status')

当我执行python manage.py db upgrade时,我得到一个错误:

And when I do python manage.py db upgrade I get an error:

...
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) type "banner_status" does not exist
LINE 1: ALTER TABLE banner ADD COLUMN status banner_status

 [SQL: 'ALTER TABLE banner ADD COLUMN status banner_status']

为什么迁移不会创建类型"banner_status"?

我在做什么错了?

$ pip freeze
alembic==0.8.6
Flask==0.10.1
Flask-Fixtures==0.3.3
Flask-Login==0.3.2
Flask-Migrate==1.8.0
Flask-Script==2.0.5
Flask-SQLAlchemy==2.1
itsdangerous==0.24
Jinja2==2.8
Mako==1.0.4
MarkupSafe==0.23
psycopg2==2.6.1
python-editor==1.0
requests==2.10.0
SQLAlchemy==1.0.13
Werkzeug==0.11.9

推荐答案

我使用它确定了这个问题.

I decided this problem using that.

我更改了迁移代码,迁移看起来像这样:

I changed code of migration and migration is look like this:

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

def upgrade():
    banner_status = postgresql.ENUM('active', 'inactive', 'archive', name='banner_status')
    banner_status.create(op.get_bind())

    op.add_column('banner', sa.Column('status', sa.Enum('active', 'inactive', 'archive', name='banner_status'), nullable=True))

def downgrade():
    op.drop_column('banner', 'status')

    banner_status = postgresql.ENUM('active', 'inactive', 'archive', name='banner_status')
    banner_status.drop(op.get_bind())

现在成功执行了python manage.py db upgrade\downgrade.

这篇关于sqlalchemy postgresql枚举不会在数据库迁移上创建类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆