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

Flask Migration: Problem creating table and using existing Enum in database. `create_type=False` Do not work

我正在使用 flask-migrate、SQLAlchemy 和 alembic 来管理我的数据库。我想在数据库中创建一个新的 table。新 table 有一个使用现有 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.Enum(),而我应该使用 postgres.ENUM()。改变使一切正常。

烧瓶升级时出现同样的错误。通过更改name属性,效果很好。

name = 'country' to name = 'country_name'

或 如果您将 Postgres 与 pgadmin4 一起使用,请删除 'country' 类型对象并重新 运行.