Repository navigation
Description
Issue Description
Model.upsert accepts a returning option since #8924. However, the way this is implemented is via a platform-specific upsert that returns the record's primary key and then a subsequent SELECT call to fetch the record.
Relevant code:
Lines 2495 to 2501 in c66663e
| .then(() => { | |
| return this.QueryInterface.upsert(this.getTableName(options), insertValues, updateValues, instance.where(), this, options); | |
| }) | |
| .then(([created, primaryKey]) => { | |
| if (options.returning === true && primaryKey) { | |
| return this.findByPk(primaryKey, options).then(record => [record, created]); | |
| } |
This is problematic for a few reasons:
-
Extra round trip to database. This should be unnecessary on platforms that support returning the full record from UPDATE/INSERTs like Postgres.
-
Subsequent read may hit a read replica instead: Upsert with returning:true and read replication returns null instance #9216
-
Use of a non-repeatable read means
upsertcan return a different result than what was actually upserted. For example, on Postgres's default READ COMMITTED transaction isolation level, a transaction may "re-reads data it has previously read and finds that data has been modified by another transaction (that committed since the initial read)." The fact that the SELECT runs in the same transaction as the INSERT/UPDATE doesn't help. Thus,upsertisn't returning the record that was created or the record as it existed as of the update, it's returning the record that existed when the subsequentfindByPkwas executed. In an extreme case, the record could have even been deleted, which would meanupsertreturnsnull.
What are you doing?
Sorry, no SSCCE since reproducing the non-repeatable read requires precise timing. It would probably require stubbing findByPk.
const [record] = Model.upsert({id: 1}, {returning: true});
// record may be null if the `destroy` call below ran in between the underlying update/insert and the subsequent findByPk
// concurrently
Model.destroy({where: {id: 1}});What do you expect to happen?
upsert returns record that was inserted
What is actually happening?
upsert returns null
Environment
- Sequelize version: master
- Node.js version: n/a
- Operating System: n/a
Issue Template Checklist
How does this problem relate to dialects?
- I think this problem happens regardless of the dialect.
- I think this problem happens only for the following dialect(s):
- I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don't know how to start, I would need guidance.
- No, I don't have the time, although I believe I could do it if I had the time...
- No, I don't have the time and I wouldn't even know how to start.
Activity
sushantdhiman commentedon Apr 18, 2020
Yes I am aware of this issue. It is mainly because not all dialect support returning.
With dialects like Postgres even though returning is supported I think we lack native upsert support, which forces use to use some syntax which can only return primary key
gabegorelick commentedon Apr 20, 2020
That makes sense. Here is the issue for native upsert in Postgres: #4132