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.

knex onConflict merge with columns

See original GitHub issue

Describe the bug

When using createKnex adapter, we have an issue with queries that do upsert on selected columns. merge without specifying columns is working fine.

Column "0" not found

To Reproduce

const data = [
        { id: 1, other_id: 111, some_field: 'value', created_at: new Date(), updated_at: new Date() },
        { id: 2, other_id: 222, some_field: 'value2', created_at: new Date(), updated_at: new Date() }
    ];
    const query = knex('example').insert(data).onConflict(['id', 'other_id']).merge(['updated_at', 'some_field']);
    console.log(query.toSQL());

it will produce do update set “0” = ?,“1” = ?’ should be do update set “some_field” = excluded.“some_field”, “updated_at” = excluded.“updated_at”’

{
  method: 'insert',
  options: {},
  timeout: false,
  cancelOnTimeout: false,
  bindings: [
    2022-01-31T21:17:53.130Z,
    1,
    111,
    'value',
    2022-01-31T21:17:53.130Z,
    2022-01-31T21:17:53.130Z,
    2,
    222,
    'value2',
    2022-01-31T21:17:53.130Z,
    'updated_at',
    'some_field'
  ],
  __knexQueryUid: 'lwEt8xzcV2xKl87iZdJQl',
  sql: 'insert into "example" ("created_at", "id", "other_id", "some_field", "updated_at") values (?, ?, ?, ?, ?), (?, ?, ?, ?, ?) on conflict ("id", "other_id") do update set "0" = ?,"1" = ?',
  returning: undefined
}

pg-mem version

2.3.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
elad-sfcommented, Feb 1, 2022

you are right… node_modules/knex version is:

"type": "version",
"version": "0.21.19"

I still waiting for npm to update pg-mem to 2.3.2, it’s still on 2.3.1 and will keep you updated. thanks!

0reactions
oguimbalcommented, Feb 1, 2022

No problem !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I use knex.js onConflict().merge() to add to an integer ...
I can "upsert" row values, adding to the some_total column, in MySQL with this: INSERT INTO `some_table` (`some_other_id`, `some_date`, ` ...
Read more >
Knex Query Builder
onConflict ().merge(). INFO. For PostgreSQL and SQLite, the column(s) specified by this method must either be the table's PRIMARY KEY or have a...
Read more >
Overriding only some merge fields · Issue #4106 · knex/knex
onConflict ().merge() but allow partial overrides of the fields. The sql I'm trying to achieve is this: INSERT INTO mytable (key_field, ...
Read more >
How to do upserts in Knex.js (PostgreSQL)
The new onConflict/ignore/merge features are available in Knex.js >= 0.21.10 and well documented here: https://knexjs.org/#Builder-onConflict. I ...
Read more >
Mutating Methods | Objection.js
Batch inserts only work on Postgres because Postgres is the only database engine that returns the identifiers of all inserted rows. knex supports...
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