website icon indicating copy to clipboard operation
website copied to clipboard

How to bulk increment integer attribute?

Open alekbarszczewski opened this issue 11 years ago • 12 comments

I want to do something like this:

User.update({ clicks: "clicks + 1" }, { type: "member" }).done(...) 

This obviously does not work - how I can write clicks = clicks + 1?

alekbarszczewski avatar May 22 '14 16:05 alekbarszczewski

Not supported currently. There are atomic updates for instances but that's currently it.

mickhansen avatar May 22 '14 17:05 mickhansen

You should be able to this though:

User.update({
  clicks: sequelize.literal('clicks +1')
,... )

janmeier avatar May 22 '14 17:05 janmeier

Perfect, it works :) Thanks For me this is enough and you can close this issue if you want.

alekbarszczewski avatar May 22 '14 20:05 alekbarszczewski

That works! Great! :+1:

renatoargh avatar Dec 12 '14 21:12 renatoargh

Thanks !!

kevingo avatar Mar 17 '16 03:03 kevingo

No, it's not working. I got this error

Unhandled rejection SequelizeDatabaseError: syntax error at or near "+"

hebbian avatar Jun 30 '16 08:06 hebbian

@hebbian You have to give some more context than that if you want us to help you - What code are you running, and what does the query look like at the least?

janmeier avatar Jun 30 '16 08:06 janmeier

@janmeier This command: like: models.sequelize.literal('like +1')

produce this query UPDATE "threads" SET "like"=like + 1,"updatedAt"='2016-06-30 08:39:39.012 +00:00' WHERE "id" = '2'

I put that query on pgadmin and it shows error: ERROR: syntax error at or near "+" LINE 1: UPDATE "threads" SET "like"=like + 1,"updatedAt"='2016-06-30...

hebbian avatar Jun 30 '16 08:06 hebbian

Try quoting (") the column name. What is the type of the column?

janmeier avatar Jun 30 '16 08:06 janmeier

@janmeier Yes, you're right, I missed that (") point. It's working now. It's an integer field .

hebbian avatar Jun 30 '16 08:06 hebbian

is this still the recommended way to do this or has there been an update?

ahmadabdul3 avatar Oct 24 '19 00:10 ahmadabdul3

@ahmadabdul3 Good question, no update that I know of, I will reopen this as a documentation issue, I think this deserves to be added to the docs

papb avatar Oct 24 '19 16:10 papb