knex onConflict merge with columns
See original GitHub issueDescribe 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:
- Created 2 years ago
- Comments:13 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

you are right… node_modules/knex version is:
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!
No problem !