gpt4 book ai didi

postgresql - JSONB sqlalchemy 聚合函数

转载 作者:行者123 更新时间:2023-11-29 12:09:33 33 4
gpt4 key购买 nike

我想使用 SQLAlchemy 重现可用的 aggregate function来自子查询的 jsonb_object_agg sq_objects:

from sqlalchemy import select, func
s = select([
sq_objects.c.object_id,
func.jsonb_object_agg(
sq_objects.c.keys, sq_objects.c.values).over(
partition_by=sq_objects.c.object_id).label("attributes"),
]).\
distinct(sq_objects.c.object_id)

但是,执行返回:

(psycopg2.ProgrammingError) can't adapt type 'method'

[ SQL: "SELECT DISTINCT ON (sq_objects.object_id)
sq_objects.object_id,
jsonb_object_agg(
%(jsonb_object_agg_1)s,
%(jsonb_object_agg_2)s
) OVER (PARTITION BY sq_objects.object_id) AS attributes
FROM (SELECT ... ) AS sq_objects"
] [
parameters: {'jsonb_object_agg_1': <bound method Properties.keys of <sqlalchemy.sql.base.ImmutableColumnCollection object at 0x7f0ffb7aa828>>,
'jsonb_object_agg_2': <bound method Properties.values of <sqlalchemy.sql.base.ImmutableColumnCollection object at 0x7f0ffb7aa828>>}]

这是我要重现的sql代码:

SELECT DISTINCT ON (sq_objects.object_id)
sq_objects.object_id,
jsonb_object_agg(
sq_objects.keys,
sq_objects.values
) OVER (PARTITION BY sq_objects.object_id) AS attributes
FROM (SELECT ... ) AS sq_objects

最佳答案

问题在于属性 keysvalues 是不可变列集合 sq_objects.c 的方法。该问题的另一种解决方案是使用项目访问符号来获取实际列:

func.jsonb_object_agg(
sq_objects.c["keys"],
sq_objects.c["values"]
).over(
partition_by=sq_objects.c.object_id
).label("attributes")

关于postgresql - JSONB sqlalchemy 聚合函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43801904/

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