gpt4 book ai didi

python - SQLAlchemy:返回column_property的默认值

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:04 26 4
gpt4 key购买 nike

我正在定义一个 SQLAlchemy column_property基于select (带有 coerced type )。如何定义要返回的 column_property 的默认类型?通常它会返回一个列表列表,但如果 select 没有找到任何内容,那么它会返回 None。相反,我希望它返回一个空列表([])。一些示例代码:

def get_list(cls_type):
select_list = select([cls_type.foo]).select_from(cls_type)
select_list = select_list.correlate_except(cls_type).label('foo_list')
coerce_array_select = select([type_coerce(func.array(select_list), ListColumnProp(Text))])
return column_property(coerce_array_select, deferred=True)

最佳答案

终于弄清楚了——这些知识嵌入在 TypeEngine 中类,在上面的例子中为 ListColumnProp。在本例中,它是通过 TypeEngine 的子类实现的 -- TypeDecorator 。例如:

class ListColumnProp(TypeDecorator):
impl = ARRAY

def process_result_value(self, value, dialect):
if value:
return [tuple(json.loads(v)) for v in value]
else:
return []

关于python - SQLAlchemy:返回column_property的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40515727/

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