gpt4 book ai didi

python - sqlalchemy func.json_build_object 抛出错误,无法确定参数的数据类型

转载 作者:行者123 更新时间:2023-12-05 06:12:20 26 4
gpt4 key购买 nike

我正在使用此 sqlalchemy 查询来获得所需格式的结果:

query = select(
[
Product.name,
Product.description,
Product.turn_around_time,
Product.id.label("product_id"),
func.array_agg(
func.json_build_object("label", Field.label ,"type", Field.field_type)
).label("product_fields")
],
).select_from(
join(Product, ProductField).join(Field)
).where(
and_(
Product.id == product_id,
Product.organization_id == user.organization_id
)
).group_by(Product.id)

这个查询是这个有效的 postgres 查询的一对一映射:

select product.name,
product.description,
product.turn_around_time,
array_agg(json_build_object('lable', field.label,'type', field.field_type))
from product
inner join product_field on product.id = product_field.product_id
inner join field on field.id = product_field.field_id
where product.id = 'f2778d60-dc63-407f-9a4e-e23595ef6dd1'
group by product.id;

这是我得到的错误:

    statement = await self._protocol.prepare(stmt_name, query, timeout)
File "asyncpg/protocol/protocol.pyx", line 163, in prepare
asyncpg.exceptions.IndeterminateDatatypeError: could not determine data type of parameter $2

但是如果我改变这一行: func.json_build_object("label", Field.label ,"type", Field.field_type) func.json_build_object( Field.label, Field.field_type) 它有效,但结果格式不同。

最佳答案

这不是最优雅的方式,但这对我有用

func.array_agg(
func.json_build_object(
text("'key', table_name.field"),
text("'key', table_name.field"),
text("'key', table_name.field")
)
).label("product_fields"),

关于python - sqlalchemy func.json_build_object 抛出错误,无法确定参数的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63655943/

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