[英]How can I select `array_agg` in `typeorm` with `postgresql`?
我在 typescript 項目中使用typeorm
連接到postresql 11
。 我在 sql 中有以下查詢,它給了我預期的結果:
select "customerUuid", array_agg("siteUuid") from "SiteCustomer" where "customerUuid" in ('id1', 'id2') group by "customerUuid";
但是當我使用typeorm
時,它總是返回空數組:
queryRunner.manager.getRepository(SiteCustomer).
.createQueryBuilder()
.select('"customerUuid", array_agg("siteUuid")')
.where('"customerUuid" IN (:customerUuids)', { customerUuids: customerUuids})
.groupBy('"customerUuid"')
.getMany();
如何讓typeorm
在 postgresql 中與array_agg
一起工作?
你可以做這樣的事情 -
let baseQuery = queryRunner.manager.getRepository(SiteCustomer)
.createQueryBuilder()
.select([
'"customerUuid" as "customerUuid"',
// It's better to specify an alias explicitly, because the default one
// is implicit and may be unexpected
'array_agg("siteUuid") as "aggregatedSiteUuids"',
])
.where('"customerUuid" IN (:customerUuids)', { customerUuids: customerUuids})
.groupBy('"customerUuid"')
但是使用 -
baseQuery.getRawMany()
如果您沒有選擇實體。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.