Chained update with increment?
See original GitHub issueSorry if I’m missing something, but I can’t seem to be able to use .increment()
along with updating other fields on the table.
For instance, I’d want to do:
knex('products')
.whereIn('id', ids)
.update({'updated_at': new Date()})
.increment('count', 1);
But running this I get:
Possibly unhandled Error: The query type has already been set to update
I understand why it happens, I just think it would be nice to be able to add other fields to the update.
Alternatively, maybe we could remove .increment()
altogether and somehow in the .update()
itself be able to reference the table’s columns. Maybe something like:
knex('products')
.whereIn('id', ids)
.update({
'updated_at': new Date(),
'count': function() {
return this + 1;
}
});
Yeah, not sure about the “this + 1” part, but maybe have another way of saying “the existing column value”. This would avoid the need for .increment()
and .decrement()
.
I realize I could write this using .raw()
, but it would be nice to have it in the library.
What do you think?
Issue Analytics
- State:
- Created 9 years ago
- Reactions:5
- Comments:17 (3 by maintainers)
Top Results From Across the Web
knex increment upsert - increment if record creation fails
Unfortunately it seems you cannot chain .increment after onConflict. I am wondering if there is a way I can do this with knex...
Read more >How to update group no incremental by 1 based on every ...
How to update group no incremental by 1 based on every group from 1 to 3 ? ... 3491, 'Valid' , 'Chain', NULL,...
Read more >How to use Auto Increment in SQL? - Edureka
To use the auto increment field, in MySQL, you have to use the AUTO_INCREMENT keyword. The starting value for AUTO_INCREMENT is 1 by...
Read more >Increment (++) - JavaScript - MDN Web Docs - Mozilla
The increment ( ++ ) operator increments (adds one to) its operand and returns the value before or after the increment, depending on...
Read more >Increment a Firestore document field - Google Cloud
Update a Firestore document field using Increment. ... Atomically increment the population of the city by 50. await washingtonRef.UpdateAsync("Regions" ...
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 FreeTop 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
Top GitHub Comments
Oh, my bad, I realized I can do this:
Sorry, disregard this issue, and thanks for the awesome lib! 😃
In addition, this would be nice:
And it would continue to be a handy option after the more difficult issue of making increment truly chainable is resolved.