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.

Multiple update queryBuilder, success in PgAdmin4 but not in typeorm

See original GitHub issue

Issue Description

Same query, work in PgAdmin4 but not in typeorm

Expected Behavior

They should provide same result, or any other method that can do multiple updates are welcome

Actual Behavior

Same query but different result, error occur in typeorm but not in PgAdmin4

Steps to Reproduce

This is my entity

@Entity()
export class EthTransaction extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({unique:true})
  txid: string;

  @Column()
  blockHeight: number;

  @Column()
  coin: string;

  @Column()
  fromAddress: string;

  @Column()
  toAddress: string;

  @Column()
  value: string;

  @Column({default:"new"})
  webhookStatus:string;

  @CreateDateColumn()
  createAt: Date;

  @UpdateDateColumn()
  updateAt: Date;
}

This is actually db eth_transaction_db

I try to do multiple records update with single query, which update webhookStatus by an array of txid I first try with typeorm

const queryBuilder = await getConnection()
.createQueryBuilder()
.update(EthTransaction)
.set({ webhookStatus: status })
.where("txid IN (:txids)", {txids:txids})
.execute()

It give me a query like this

queryBuilder [
  'UPDATE "eth_transaction" SET "webhookStatus" = $1, "updateAt" = CURRENT_TIMESTAMP WHERE "txid" IN ($2)',
  [
    'pending',
    [
      '0x8bef57e3ee1b35e4a02286fc4a21ddd8392f8e215685e275b9b65f4aac6a2219',
      '0x1074e795c79d7f2ffb749252421a61f69bab14cc80eed43e45ca89ac7c684a49'
    ]
  ]
]

And error comes

error QueryFailedError: could not determine data type of parameter $1
    at new QueryFailedError (/home/project/node_modules/typeorm/error/QueryFailedError.js:11:28)
    at PostgresQueryRunner.<anonymous> (/home/project/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:247:31)
    at step (/home/project/node_modules/typeorm/node_modules/tslib/tslib.js:141:27)
    at Object.throw (/home/project/node_modules/typeorm/node_modules/tslib/tslib.js:122:57)
    at rejected (/home/project/node_modules/typeorm/node_modules/tslib/tslib.js:113:69)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  length: 111,
  severity: 'ERROR',
  code: '42P18',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'postgres.c',
  line: '1469',
  routine: 'exec_parse_message',
  query: 'UPDATE "eth_transaction" SET "webhookStatus" = $2, "updateAt" = CURRENT_TIMESTAMP WHERE "txid" IN ($2)',
  parameters: [
    'pending',
    [
      '0x8bef57e3ee1b35e4a02286fc4a21ddd8392f8e215685e275b9b65f4aac6a2219',
      '0x1074e795c79d7f2ffb749252421a61f69bab14cc80eed43e45ca89ac7c684a49'
    ]
  ]
}

But when i do the same query in PgAdmin 4, it pass and did the job

UPDATE "eth_transaction" SET "webhookStatus" = 'pending', "updateAt" = CURRENT_TIMESTAMP WHERE "txid" IN ('0x8bef57e3ee1b35e4a02286fc4a21ddd8392f8e215685e275b9b65f4aac6a2219', '0x1074e795c79d7f2ffb749252421a61f69bab14cc80eed43e45ca89ac7c684a49');

My Environment

Ubuntu 20.04
nodejs v12.19.0
typescript
nestjs 7.5.1
@nestjs/typeorm": "^7.1.5
typeorm": "^0.2.30
typescript": "^4.0.5
@typescript-eslint/eslint-plugin": "^4.6.1
@typescript-eslint/parser": "^4.6.1

Additional Context

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don’t know how to start. I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
asomethingscommented, Jan 28, 2021
const queryBuilder = await getConnection()
  .createQueryBuilder()
  .update(EthTransaction)
  .set({ webhookStatus: status })
  .where("txid IN (:...txids)", {txids:txids})
  .execute()

You are trying to send array as an “array”. You have to make it unpack through typeorm. Above example should work as expected.

0reactions
jackykwandesigncommented, Feb 1, 2021

@jackykwandesign It’s actually in the doc although it’s select but still it’s an query builder so I think people will think it’s compatible

Thanks for your reminder

Read more comments on GitHub >

github_iconTop Results From Across the Web

nestjs - How to update many fields on a table without using ...
Yes, it is possible to convert your QueryBuilder update query into a Repository one. For this, you could use the In operator provided...
Read more >
PostgreSQL WHERE NOT EQUAL Clause - ObjectRocket
For example, UPDATE , DELETE , and ORDER BY are statements that pair with the WHERE clause and there are others. Let's look...
Read more >
/CHANGELOG.md | typeorm@v0.2.23-rc10 | Deno
now update method in QueryBuilder accepts Partial<Entity> and property names used in update map are column property names and they are automatically mapped...
Read more >
Update using Query Builder - typeorm - GitBook
You can create · This is the most efficient way in terms of performance to update entities in your database. · In some...
Read more >
update for multiple columns in sql Code Example
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY...
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