Flask迁移:在创建表和使用数据库中现有的Enum时出现问题。 `create_type = False`不起作用

问题描述 投票:0回答:1

我正在使用flask-migrate,SQLAlchemy和Alembic来管理我的数据库。我想在数据库中创建一个新表。新表具有使用现有Enum的列。我读过许多关于SO的问题,您可以将现有的Enumcreate_type=False标志一起使用。这似乎对我不起作用。请参阅下面我的修订文件中的upgrade()功能。

def upgrade():
    op.create_table(
        'label',
        sa.Column('id', UUID(as_uuid=True), default=uuid4),
        sa.Column('labelText', sa.Text, nullable=False),  
        sa.Column('sourceCountry', sa.Enum('it', 'gb', 'gr', 'bg', 'pt', name='country', create_type=False), nullable=True),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('id')

    )
    op.add_column('entity', sa.Column('labelId', UUID(as_uuid=True)))
    op.create_foreign_key(
        'fk_entity_label',
        'entity', 'label',
        ['labelId'], ['id'],
    )

这是我的版本:

Flask==1.1.1
Flask-Ext==0.1
Flask-Migrate==2.5.3
Flask-Script==2.0.6
Flask-SQLAlchemy==2.4.1
alembic==1.4.1

我的错误:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DuplicateObject) type "country" already exists

[SQL: CREATE TYPE country AS ENUM ('it', 'gb', 'gr', 'bg', 'pt')]
(Background on this error at: http://sqlalche.me/e/f405)
sqlalchemy flask-sqlalchemy alembic flask-migrate
1个回答
0
投票

发现了问题。我使用的是sqlalchemy.Enum(),我应该改用postgres.ENUM()。改变使一切正常。

© www.soinside.com 2019 - 2024. All rights reserved.