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.

Chained update with increment?

See original GitHub issue

Sorry 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:closed
  • Created 9 years ago
  • Reactions:5
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

21reactions
imiriccommented, May 12, 2014

Oh, my bad, I realized I can do this:

knex('products')
  .whereIn('id', ids)
  .update({
    'updated_at': new Date(),
    'count': knex.raw('count + 1')
  });

Sorry, disregard this issue, and thanks for the awesome lib! 😃

15reactions
boutellcommented, Feb 18, 2017

In addition, this would be nice:

.increment({
  'thumbs_up': 1,
  'thumbs_down': -1
})

And it would continue to be a handy option after the more difficult issue of making increment truly chainable is resolved.

Read more comments on GitHub >

github_iconTop 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 >

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