带有case子句和枚举的SQLAlchemy更新记录

时间:2017-07-21 12:39:11

标签: python enums sqlalchemy python-3.4

我定义了以下模型和enum

class StatusEnum(enum.Enum):
    NEW = 'NEW'
    PROCESSED = 'PROCESSED'
    IN_PROGRESS = 'IN_PROGRESS'

class RequestLog(Base):
    __tablename__ = 'request_log'

    ...
    status = Column(Enum(StatusEnum))
    ...

我正在尝试以下列方式更新记录:

>>> session.query(RequestLog).filter(RequestLog.id.in_([8])).update(
{'status': case(
    [(RequestLog.attempt_done_count == RequestLog.attempt_count - 1, StatusEnum.PROCESSED)],
    else_=StatusEnum.IN_PROGRESS)},
synchronize_session=False)

在此期间,我收到了一个错误:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'StatusEnum' [SQL: 'UPDATE request_log SET status=CASE WHEN (request_log.attempt_done_count = request_log.attempt_count - %(attempt_count_1)s) THEN %(param_1)s ELSE %(param_2)s END WHERE request_log.id IN (%(id_1)s)'] [parameters: {'param_2': <StatusEnum.IN_PROGRESS: 'IN_PROGRESS'>, 'attempt_count_1': 1, 'param_1': <StatusEnum.PROCESSED: 'PROCESSED'>, 'id_1': 8}]

1 个答案:

答案 0 :(得分:2)

对于一个简单的Python枚举对象绑定值,运行bind processor of sqltypes.Enum,问题为uses the string name of the enum object

In [27]: session.query(RequestLog).filter(RequestLog.id.in_([8])).update(
    ...:     {'status': StatusEnum.NEW},
    ...:     synchronize_session=False)
2017-07-24 15:15:43,848 INFO sqlalchemy.engine.base.Engine UPDATE request_log SET status=%(status)s WHERE request_log.id IN (%(id_1)s)
INFO:sqlalchemy.engine.base.Engine:UPDATE request_log SET status=%(status)s WHERE request_log.id IN (%(id_1)s)
2017-07-24 15:15:43,848 INFO sqlalchemy.engine.base.Engine {'status': 'NEW', 'id_1': 8}
INFO:sqlalchemy.engine.base.Engine:{'status': 'NEW', 'id_1': 8}
Out[27]: 0

对于SQL表达式,这似乎不是递归发生的,所以在你的case()中,enum对象的绑定值被传递给psycopg,后者不知道如何处理它们。要模拟SQL表达式中的行为,您可以使用适当的强制转换手动传递names of the enum objects

In [60]: session.query(RequestLog).filter(RequestLog.id.in_([8])).update(
    ...:     {'status': case(
    ...:         [(true(), StatusEnum.PROCESSED.name)],
    ...:         else_=StatusEnum.IN_PROGRESS.name).cast(RequestLog.status.type)},
    ...:     synchronize_session=False)
2017-07-24 15:40:52,853 INFO sqlalchemy.engine.base.Engine UPDATE request_log SET status=CAST(CASE WHEN true THEN %(param_1)s ELSE %(param_2)s END AS statusenum) WHERE request_log.id IN (%(id_1)s)
INFO:sqlalchemy.engine.base.Engine:UPDATE request_log SET status=CAST(CASE WHEN true THEN %(param_1)s ELSE %(param_2)s END AS statusenum) WHERE request_log.id IN (%(id_1)s)
2017-07-24 15:40:52,853 INFO sqlalchemy.engine.base.Engine {'param_2': 'IN_PROGRESS', 'param_1': 'PROCESSED', 'id_1': 8}
INFO:sqlalchemy.engine.base.Engine:{'param_2': 'IN_PROGRESS', 'param_1': 'PROCESSED', 'id_1': 8}
Out[60]: 0

这是不雅观的,我确信有更好的方法,但暂时这是我能想到的最好的方法。