英文:
Problem with updating an ENUM type in postgresql and alembic
问题
我在postgresql和alembic中有一个关于ENUM
的问题,我无法通过现有的StackOverflow主题解决这个问题。
我有以下的代码:
from sqlalchemy.dialects.postgresql import ENUM
from enum import Enum
class StatusEnum(Enum):
requested = "requested"
accepted = "accepted"
declined = "declined"
class EventModifications(BaseModel):
__tablename__ = "event_modifications"
old_value = Column(Text)
new_value = Column(Text)
status = Column(
ENUM(StatusEnum),
default=StatusEnum.accepted.value,
server_default=StatusEnum.accepted.value
)
然后我在枚举类中添加了一个实体,并且也将status
列的默认值更改为新添加的值:
class StatusEnum(Enum):
requested = "requested"
accepted = "accepted"
declined = "declined"
modified = "modified" # 新添加的
class EventModifications(BaseModel):
__tablename__ = "event_modifications"
old_value = Column(Text)
new_value = Column(Text)
status = Column(
ENUM(StatusEnum),
default=StatusEnum.modified.value, # 更改了默认值
server_default=StatusEnum.modified.value
)
Alembic引发的错误:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DatatypeMismatch) column "status" cannot be cast automatically to type statusenum
HINT: You might need to specify "USING status::statusenum".
[SQL: ALTER TABLE event_modifications ALTER COLUMN status TYPE statusenum ]
但是当我将StatusEnum
类的名称更改为其他名称时,它正常工作!
英文:
I have an issue with ENUM
in postgresql and alembic and I couldn't resolve the problem using the existing topics in the StackOverflow in this regard.
I had the following code:
from sqlalchemy.dialects.postgresql import ENUM
from enum import Enum
class StatusEnum(Enum):
requested = "requested"
accepted = "accepted"
declined = "declined"
class EventModifications(BaseModel):
__tablename__ = "event_modifications"
old_value = Column(Text)
new_value = Column(Text)
status = Column(
ENUM(StatusEnum),
default=StatusEnum.accepted.value,
server_default=StatusEnum.accepted.value
)
Then I appended an entity in the Enum class and also changed the default value of status
column to the new added value:
class StatusEnum(Enum):
requested = "requested"
accepted = "accepted"
declined = "declined"
modified = "modified" # new one
class EventModifications(BaseModel):
__tablename__ = "event_modifications"
old_value = Column(Text)
new_value = Column(Text)
status = Column(
ENUM(StatusEnum),
default=StatusEnum.modified.value, # changed the defaults
server_default=StatusEnum.modified.value
)
Error caused by Alembic:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DatatypeMismatch) column "status" cannot be cast automatically to type statusenum
HINT: You might need to specify "USING status::statusenum".
[SQL: ALTER TABLE event_modifications ALTER COLUMN status TYPE statusenum ]
But when I change the name of StatusEnum
class to something else it works well!
答案1
得分: 2
你可以使用以下查询来解决你的问题:
ALTER TABLE "event_modifications" DROP "status";
DROP TYPE "statusenum"
- 请注意你的数据将被删除,你需要再次调用alembic来进行迁移。
英文:
You can use these queries to solve your problem
1.
ALTER TABLE "event_modifications" DROP "status";
2.
DROP TYPE "statusenum"
- note that your data will be removed and you need call alembic again to migratation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论