尝试更新数据库模型时,我遇到了无效枚举输入的问题。错误如下。我不确定我做错了什么
sqlalchemy.exc.DataError: (psycopg2.errors.InvalidTextRepresentation) invalid input value for enum recipientstatus
这是我使用的枚举和模型
class RecipientStatus(enum.Enum):
WAITING_TO_SEND = "WAITING_TO_SEND"
PENDING_TO_SEND = "PENDING_TO_SEND"
class Recipient(db.Model):
status = db.Column(db.Enum(RecipientStatus))
下面的代码是我希望枚举执行的操作的示例。由于某种原因,它无法识别代码的枚举
for recipients in recipients:
recipient.status = RecipientStatus.FAILED_TO_SEND.value
recipient.status_changed_at = datetime.now(timezone.utc)
db.session.commit()
如果有帮助的话,这是 alembic 命令
def upgrade():
recipient_status_enum = ENUM(
"WAITING_TO_SEND",
"PENDING_TO_SEND",
name="recipientstatus",
)
recipient_status_enum.create(op.get_bind(), checkfirst=True)
op.add_column(
"recipient", sa.Column("status", recipient_status_enum, nullable=True)
)
我尝试更改枚举。我尝试不使用 ENUM 的 .value,但它仍然给我一个错误
看起来
FAILED_TO_SEND
不是您编写的枚举的一部分:
class RecipientStatus(enum.Enum):
WAITING_TO_SEND = "WAITING_TO_SEND"
PENDING_TO_SEND = "PENDING_TO_SEND"
尝试添加并再次运行。