sequelize icon indicating copy to clipboard operation
sequelize copied to clipboard

returning is ignored for MSSQL update

Open smassin opened this issue 7 years ago • 5 comments

What are you doing?

I'm trying to execute an update using the MSSQL driver without having an OUTPUT INSERTED.* appended but returning: false is ignored. However, this is inconsistent with the insertQuery definition which does respect returning: false for MSSQL.

instance.update({
   name : 'Name'
}, {
   returning: false
})

To Reproduce Steps to reproduce the behavior:

  1. Execute the above statement on any model using MSSQL
  2. See the SQL output will include OUTPUT INSERTED.*.

What do you expect to happen?

Not to have OUTPUT INSERTED.*.

What is actually happening?

The output includes OUTPUT INSERTED.*.

As mentioned above this seems like a mistake and is inconsistent with the insert query which does respect the returning flag.

Environment

Dialect:

  • [x] mssql Latest master branch

smassin avatar Aug 09 '19 21:08 smassin

[...] insertQuery definition which does respect returning: false

Can you show a example snippet for this?

papb avatar Aug 10 '19 05:08 papb

insertQuery

https://github.com/sequelize/sequelize/blob/a39c63a44201e13a44a75e6acd7ba3b08d7643d7/lib/dialects/abstract/query-generator.js#L132

    if (this._dialect.supports.returnValues && options.returning) {
      if (this._dialect.supports.returnValues.returning) {
      } else if (this._dialect.supports.returnValues.output) {
      }
    }

updateQuery

https://github.com/sequelize/sequelize/blob/a39c63a44201e13a44a75e6acd7ba3b08d7643d7/lib/dialects/abstract/query-generator.js#L367

    if (this._dialect.supports.returnValues) {
      if (this._dialect.supports.returnValues.output) {
      } else if (this._dialect.supports.returnValues && options.returning) {
      }
    }

Was expecting

    if (this._dialect.supports.returnValues && options.returning) {
      if (this._dialect.supports.returnValues.output) {
      } else if (this._dialect.supports.returnValues) {
      }
    }

In the case with insert you see that both paths (the PG and MSSQL paths) are gated by options.returning. In the update case only the PG path is gated.

On another note, I know why this may currently be the case. It seems that "affected rows" doesn't work right when OUTPUT is not part of it. For example, if the above change is made Model.update() will always return 0 affected rows even when there were. I haven't yet looked into that one further. Doesn't really make sense because other query engines can get the # of affected rows without the OUTPUT statement. Either way this secondary issue could be avoided by making returning = true by default for MSSQL for the update call and optionally set to false.

Edit: Looking at the update statement further that series of if statements is whack. There is a double check for this._dialect.supports.returnValues while the insert code checks this._dialect.supports.returnValues.returning instead in the second if. There's no comments around it so I can't tell if this is intentional or not.

smassin avatar Aug 12 '19 14:08 smassin

Thanks for the investigation and detailed explanation :+1: would you be willing to send a PR?

papb avatar Aug 12 '19 21:08 papb

The issue is actually more deep that it appears without breaking backward compatibility.

Initially I tried simply adding returning !== false to the if (this._dialect.supports.returnValues.output) to keep backward compat but that didn't work. because update defaults returning = false. That means in the current code update will use OUTPUT even when returning is false.

I later tried adding a default of true to mssql instead so that adding a returning !== false would work but no. That has additional implications around affectedRows which I haven't yet looked into enough.

It seems like any fix would be a breaking change so I'm not sure how to proceed. Also, increment as the same issue. I'm a bit confused myself cause Model.update seems to require OUTPUT for returning the correct # of affected rows but when looking at code in the global query function it can also calculate this without needing the rows returned... Any idea why this is?

For the moment I just created a new property called "outputting" so I could continue with my work.

FYI: The only reason this is an issue is because I'm using a view with a join and apply an update to the view directly. MSSQL complains when you use INSERTED.* on a view because the update only affected one of the tables but you want results from all joined tables. For my purposes I could also just write code to change the underlying table directly and refresh the view instance. However, it still seems like this returning code should be done right.

smassin avatar Aug 19 '19 16:08 smassin

@smassin Thanks for the further investigation.

but when looking at code in the global query function it can also calculate this without needing the rows returned...

How?

However, it still seems like this returning code should be done right.

Agreed :+1:

It seems like any fix would be a breaking change so I'm not sure how to proceed.

Please continue investigating, if you can really make a case to make this breaking change, then we do a breaking change. But better to triple-check everything first.

Thanks again for the help so far! Hopefully you'll figure it out for us :)

papb avatar Aug 19 '19 20:08 papb