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

2024-02-15

我正在使用此 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) to 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"),
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

sqlalchemy func.json_build_object 抛出错误,无法确定参数的数据类型 的相关文章

随机推荐