gpt4 book ai didi

types - SQL Alchemy - 使用 TypeDecorator 进行模式提取

转载 作者:行者123 更新时间:2023-12-02 01:59:13 25 4
gpt4 key购买 nike

我正在使用 TypeDecorator 进行 Json 提取,而另一个模型将其用于其列之一。我正在使用这个 TypeDecorator 存储 python 列表对象。

def process_bind_param(self, value, dialect):
# etc...

def process_result_value(self, value, dialect):
# THIS NEVER GETS CALLED!!
if value is not None:
return json.loads(value)
return value

当我在使用装饰器的模型中存储数据时,会适本地调用 bind_param。现在,我通过以下方式使用 TypeDecorator 从模型中提取模式:

table = Table(table_name, meta, autoload=True, autoload_with=sengine)

现在进行查询测试(有很多方法可以循环和提取):

for record in source.query(table).all():
print type(record.column_using_custom_type_list_object) == str
# returns true ... this should be false ... should be of type list
# json.loads() returns type list ???
print record.column_using_custom_type_list_object[some_index]
# naturally this prints a character in the string, not a cell

问题是在查询表、对象和列时未调用 process_result_value()。我假设 SQLAlchemy 反射处理依赖项?我是否在构造函数中遗漏了一些选项来传输需要自定义类型装饰器的元数据?

最佳答案

我不确定这是否是同一个问题,但我认为它的相关性足以让我在这里写下答案。

存储对象并尝试读取属性后,函数 process_result_value 没有被调用,因为对象被缓存了。 (在这里找到 http://comments.gmane.org/gmane.comp.python.sqlalchemy.user/11406 )

所以解决方案就是使对象无效。

session.expire(obj)

http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html#refreshing-expiring

关于types - SQL Alchemy - 使用 TypeDecorator 进行模式提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18153618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com