我正在使用烧瓶迁移,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'],
)以下是我的版本:
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)发布于 2020-03-20 14:51:42
发现问题了。我在使用sqlalchemy.Enum(),而应该使用postgres.ENUM()。改变这一切让一切都正常。
发布于 2020-07-18 13:44:21
在烧瓶升级过程中也会发生同样的错误。通过更改name属性,它工作得很好。
name = 'country' to name = 'country_name'或者,如果您在pgadmin4中使用Postgres,请删除“country”类型的对象并重新运行。
https://stackoverflow.com/questions/60763478
复制相似问题