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

Dar*_*ioB 3 sqlalchemy flask-sqlalchemy alembic flask-migrate

我正在使用flask-migrate、SQLAlchemy 和alembic 来管理我的数据库。我想在数据库中创建一个新表。新表有一列使用现有的Enum. 我读过很多这样的问题,你可以使用现有的Enum带有create_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'],
    )
Run Code Online (Sandbox Code Playgroud)

这是我的版本:

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
Run Code Online (Sandbox Code Playgroud)

我的错误:

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)
Run Code Online (Sandbox Code Playgroud)

Dar*_*ioB 5

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