sequelize / sequelize

Feature-rich ORM for modern Node.js and TypeScript, it supports PostgreSQL (with JSON and JSONB support), MySQL, MariaDB, SQLite, MS SQL Server, Snowflake, Oracle DB (v6), DB2 and DB2 for IBM i.
https://sequelize.org/
MIT License
29.52k stars 4.27k forks source link

Updating multiple rows, but returning single row #7628

Closed chauhanshubham-dev closed 7 years ago

chauhanshubham-dev commented 7 years ago

What I'm doing? For one of my use-cases I want to update all rows, having a matching field, and return all updated rows.

What's going wrong? The code below, returns only a single updated row, and is actually updating all the required rows. So, the problem seems to be in returningn rows only.

Code snippet models.SomeTable.update({ status: 'pending' }, { where: { myForeignKeyField: 123 }, returning: true, plain: true }).then((tableElements) => { console.log(tableElements[1].dataValues) // returning only a single object // would be using returned tableElement Ids })

Dialect: postgres Database version: psql (PostgreSQL) 9.6.2 Sequelize version: 4.1.2

Seems to be an issue, that should have been taken care of, but don't know why it's not working for me. Let me know if you require something else.

sushantdhiman commented 7 years ago

Update method returns Promise<Array<affectedCount, affectedRows>> You need to use 2nd argument to access returned records.

Please read docs http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-update