question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using "in" operator with uuid types causes "invalid input syntax" error

See original GitHub issue

Issue type:

  • bug report

Database system/driver:

  • postgres v11

TypeORM version:

  • latest 0.2.26

With postgres, “in” operator is valid on UUID types:

SELECT x.* FROM my_table x WHERE x."_id" IN ('77dec6ee-8d39-4b73-825a-6190628d2953','7900b8c6-c239-45e1-9ca8-2d1a244eb881')

However, the following code throws an error:

repository.find({
    where: {
      _id: In(['77dec6ee-8d39-4b73-825a-6190628d2953', '7900b8c6-c239-45e1-9ca8-2d1a244eb881']),
    }
});
QueryFailedError invalid input syntax for type uuid: "{"_type":"in","_value":["77dec6ee-8d39-4b73-825a-6190628d2953", "7900b8c6-c239-45e1-9ca8-2d1a244eb881"],"_useParameter":true,"_multipleParameters":true}"

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:21 (7 by maintainers)

github_iconTop GitHub Comments

7reactions
gpmarchicommented, Nov 17, 2021

I believe the answer to this question is in this other issue: https://github.com/typeorm/typeorm/issues/1985

All you have to do is use the new ‘…’ operator to spread the array inside the In clause of your query, like so:

export const queryBatchFunction = <T extends { id: string }>(repository: Repository<T>) => async (
  keys: string[],
): Promise<(T | null)[]> => {
  const docs = await repository
    .createQueryBuilder("TableName")
    .where(`TableName.id IN (:...keys)`, { keys })
    .getMany();

  return keys.map(k => docs.find(d => d.id === k) || null);
};

Note the :...key thing, but I don’t see this’s being mentioned anywhere in the changelog… Am I missing anything?

_Originally posted by @lednhatkhanh in https://github.com/typeorm/typeorm/issues/1985#issuecomment-383270029_

2reactions
miedzikdcommented, Jun 20, 2021

Similar effect for inside query builder…

qb.andWhere("v.customer IN (:customers)", { customers: ["548b47a3-61b9-48f4-b1f2-eb83011a20d5"]});

throwing this error:

UnhandledPromiseRejectionWarning: QueryFailedError: invalid input syntax for type uuid: "{"548b47a3-61b9-48f4-b1f2-eb83011a20d5"}"
app_1      |     at new QueryFailedError (/app/src/error/QueryFailedError.ts:9:9)
app_1      |     at Query.callback (/app/src/driver/postgres/PostgresQueryRunner.ts:178:30)
app_1      |     at Query.handleError (/app/node_modules/pg/lib/query.js:128:19)
app_1      |     at Client._handleErrorMessage (/app/node_modules/pg/lib/client.js:335:17)
app_1      |     at Connection.emit (events.js:314:20)
app_1      |     at Connection.EventEmitter.emit (domain.js:483:12)
app_1      |     at /app/node_modules/pg/lib/connection.js:115:12
app_1      |     at Parser.parse (/app/node_modules/pg-protocol/src/parser.ts:102:9)
app_1      |     at Socket.<anonymous> (/app/node_modules/pg-protocol/src/index.ts:7:48)
app_1      |     at Socket.emit (events.js:314:20)

Interestingly, the search will work if I add the parameter value inside the query, but only if I use single quotes like here:

qb.andWhere("v.customer IN ('548b47a3-61b9-48f4-b1f2-eb83011a20d5')");
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nestjs - QueryFailedError: invalid input syntax for type uuid
I ran into a similar error and kept landing back on this issue. Eventually discovered (via github.com/nestjs/nest/issues/1667) that the ordering ...
Read more >
typeorm/typeorm - Gitter
When I try to execute the query, I get invalid input syntax for type timestamp: ... findOne() from the database and then save...
Read more >
BUG #17139: Invalid input syntax for uuid (politely this time!)
ones. It says: ERROR: invalid input syntax for type uuid: Aside from the lack of the literal word "value" it seems to meet...
Read more >
ERROR: invalid input syntax for type uuid on wrong column
I need to write a function because I am using postgraphile to make the request to insert the data. Here is my SQL...
Read more >
ORA-40001 to ORA-40999 - Oracle Help Center
ORA-40102: invalid input string for data mining operation string. Cause: The input parameter was ... Cause: The expression had syntax or semantic errors....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found