简体   繁体   中英

Converting a PostgresSQL query into Knex.js

I have to use the LOWER function in whereIn using knex. I want to change the below line of code

whereIn("name", members);

If I do whereIn('LOWER("name")', members); , getting error as Column LOWER("name") is not present. I tried to use knex.raw but again not getting the correct syntax.

const roleMembers = await t("roles")
          .select({ role_id: "id" }, "name")
          .whereIn(t.raw("LOWER('name')"), members);

Error: 在此处输入图片说明

index.d.ts(1184, 5)

When I try to add await, syntax error goes away but when I execute this code I get the below error

error: LOWER(name) - syntax error at or near "LOWER" I'm referring http://knexjs.org/#Builder-whereIn

We have whereRaw , Do we have a similar thing for whereIn ? Need some help to fix this issue.

Note: I used all LOWER, lower, Lower.

knex.raw()可用于列组件

knex('members').whereIn(knex.raw('Lower("name")'), members)
select * from `members` where Lower("name") in ('Tom', 'Dick', 'harry')

The final solution is below.

const roleMembers = await t("roles")
          .select({ role_id: "id" }, "name")
          .whereIn(t.raw('LOWER("name")') as any, members);

My changes to the original code are

  1. Quotes: swapped " and '
  2. lower, Lower and LOWER worked
  3. Typescript error: added as any

Thanks Matt and Mikael for guidance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM