首页 > 解决方案 > 无法搜索字符变 [] 数组列

问题描述

我的 postgres 数据库表有一个列位置,它是 character varying[]。在我的表格的nestjs实体中,我关注位置列-

 @Column("character varying",{array:true})
    location: string[];

我想要做的是搜索将参数作为位置传递的行。这是给我适当结果的原始查询-

select * from blogs where language @> '{"Spanish","English"}'

在我的nestjs服务中,如何实现上述查询?我试过这样做-

return await this.blogsRepo.find({
  where: [
    {
      location: Any(body.locations)
    }
  ]
})

body.locations是这样的数组-

body.locations = ["Spanish","English"]

上面的 typeorm 解决方案给了我以下错误-

'找不到数据类型字符变化 [] 的数组类型'

可能的解决方案是什么?我会喜欢 typeorm 解决方案,因为我将原始查询执行作为我的最后一个选择。

提前致谢,

标签: postgresqlnestjstypeorm

解决方案


你可以试试这个:

await this.blogsRepo.find({
  where: `${body.locations} = ANY (location)`,
});

推荐阅读