我有一个子类myTypeSA
的sqlalchemy.types.TypeDecorator
,它定义了process_bind_param
和process_result_value
,以允许myType
实例持久化到数据库:
class myTypeSA(types.TypeDecorator):
""" Decorate String type to translate myType instance into String in the database"""
impl = types.String(15)
def process_bind_param(self, value, dialect):
if value is None:
return None
assert type(value) == myType
return value.as_str()
def process_result_value(self, value, dialect):
if value is None:
return None
return myType.from_str(value)
在提交的某个时刻,sqlalchemy似乎试图查看对象是否已更改,并调用TypeDecorator.compare_values
,默认为sa.字符串compare_values
,即TypeEngine.compare_values
。这实际上只是x == y
,在我的例子中是这样断言的,因为x是myType
的一个实例,y是一个sqlalchemy字符串:NO_VALUE
。你知道吗
我很想直接重写TypeDecorator.compare_values
来解决这个问题,但我想知道我是否遗漏了什么,应该重写coerce_compared_value
或process_literal_param
;详细说明何时使用这些方法的注释对我来说不是很清楚。与NO_VALUE
的比较应该如何处理?你知道吗
谢谢
目前没有回答
相关问题 更多 >
编程相关推荐