【问题标题】:Adding OR condition with other than entity column in typeorm gives error: could not determine data type of parameter $2在 typeorm 中使用非实体列添加 OR 条件会给出错误:无法确定参数 $2 的数据类型
【发布时间】:2021-05-10 12:06:01
【问题描述】:

我想生成如下 postgres 查询:

select * from table1 t1 inner join table2 t2 on t1.id = t2.table1_id
where param.name is null or t1.name ilike param.name

这里的param是请求参数

我试过这个:

await this.userRepository.createQueryBuilder("t1")
        .innerJoinAndSelect("t1.table2","t2")
        .where("t1.name ilike :name or :name is null",{ name:`%${request.body.name}%`)
        .getMany()

这会返回以下错误:

QueryFailedError: 无法确定参数 $2 的数据类型

【问题讨论】:

标签: node.js typescript postgresql typeorm


【解决方案1】:

我使用以下查询来满足我的需要:

    await this.userRepository.createQueryBuilder("t1")
               .andWhere(new Brackets(q => {
                    q.where("d.name ilike :name", { name:`%${request.body.name}%`})                      
                    q.orWhere("cast(:param as character varying) is null", { param: request.body.name || null})
                })) 

【讨论】:

    【解决方案2】:

    我刚刚尝试了这个例子,得到了同样的错误,根据我的经验,我认为这是 typeorm 的一个错误

    我建议你改变解决问题的方法,试试这个:

    await this.userRepository.createQueryBuilder("t1")
            .innerJoinAndSelect("t1.table2","t2")
            .where("t1.name ilike :name",{ name: req.body.name ? `%${request.body.name}%` : '%%')
            .getMany()
    

    这将与提供的查询相同。

    【讨论】:

      猜你喜欢
      • 2021-08-05
      • 2021-07-28
      • 1970-01-01
      • 2018-02-19
      • 2018-08-20
      • 2019-12-10
      • 2017-10-29
      • 2014-08-19
      • 1970-01-01
      相关资源
      最近更新 更多