Home > other >  Typeorm/Nestjs Raw query usage forcing lowercase to column name
Typeorm/Nestjs Raw query usage forcing lowercase to column name

Time:11-06

I need a help on executing a query using nest/typeorm;

Im using Typeorm "InjectConnection" to build a raw query into my Postgres Database, the field giving me the error is the column user_roles_role.userId (note that I from userId is uppercase)
Heres the code:

const queryText = `SELECT * FROM user_roles_role WHERE user_roles_role.userId = ${id}`

try {
  const rawData = await this.connection.query(queryText);
  return rawData;
} catch (err) {
  console.log(err);
  return err;
}

I get an error while doing this query, because somehow Typeorm is forcing lowercase on the column name, as seen below on typeorm error (from catch(Err))

query: 'SELECT * FROM user_roles_role WHERE user_roles_role.userId = 1', parameters: undefined, driverError: error: column user_roles_role.userid does not exist

I've tried:

Using single quotes and double quotes (didn't work)

Full Error:

"query": "SELECT * FROM user_roles_role WHERE user_roles_role.userId = 1", "driverError": { "length": 189, "name": "error", "severity": "ERROR", "code": "42703", "hint": "Perhaps you meant to reference the column >"user_roles_role.userId".", "position": "37", "file": "parse_relation.c", "line": "3599", "routine": "errorMissingColumn" }

CodePudding user response:

as PostgreSQL sensitive case, you should make the colmun on double quotes like this:

 `SELECT * FROM user_roles_role WHERE user_roles_role."userId" = ${id}`
  • Related