Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model.save({ where }) #11254

Open
gabegorelick opened this issue Jul 30, 2019 · 4 comments
Open

model.save({ where }) #11254

gabegorelick opened this issue Jul 30, 2019 · 4 comments
Labels
hacktoberfest status: understood For issues. Applied when the issue is understood / reproducible. type: feature For issues and PRs. For new features. Never breaking changes.

Comments

@gabegorelick
Copy link
Contributor

gabegorelick commented Jul 30, 2019

Is your feature request related to a problem? Please describe.
I want to save a model, but only if a condition still holds. An example of this would be versioning, which model.save already supports internally, but a where clause is more general.

Describe the solution you'd like
model.save({where})

Why should this be in Sequelize
model.save internally constructs a WHERE clause consisting of the model's primary key and, optionally, versioning. The ability to add to that is very useful.

Describe alternatives you've considered
Versioning is already supported by Sequelize, but it requires cooperation from the model (needs to have versioning enabled). It's also much more limited.

Model.update can be used, but requires manually adding the instance's primary key to the where clause:

Model.update({
  where: {
    id: instance.id,// need to make sure you set the primary key correctly!
    condition: ...
   }
});

Usage example

await model.destroy({force: false});
model.foo = 'bar';

// Only update if model is still deleted
await model.save({where: {deletedAt: model.deletedAt}});

Additional context
Would need to figure out what should happen if caller specified primary key in where. Should that override the primary key that model.save sets internally, or throw an error? Same for versioning.

@papb
Copy link
Member

papb commented Jul 30, 2019

What is this "versioning" you are referring to?

@papb papb added status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: feature For issues and PRs. For new features. Never breaking changes. labels Jul 30, 2019
@gabegorelick
Copy link
Contributor Author

What is this "versioning" you are referring to?

https://sequelize.org/master/manual/models-definition.html#optimistic-locking

sequelize/lib/model.js

Lines 3828 to 3830 in c32ac01

if (versionAttr) {
values[versionFieldName] = parseInt(values[versionFieldName], 10) + 1;
}

@papb papb added status: understood For issues. Applied when the issue is understood / reproducible. and removed status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action labels Jul 30, 2019
@github-actions
Copy link
Contributor

github-actions bot commented Nov 8, 2021

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

@ephys
Copy link
Member

ephys commented Jun 28, 2023

This issue is strongly related to #16158, where we discussed making the method throw an OptimisticLockError (or similar) if the condition matched no records

@ephys ephys changed the title model.save({ where }) model.save({ where }) Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest status: understood For issues. Applied when the issue is understood / reproducible. type: feature For issues and PRs. For new features. Never breaking changes.
Projects
None yet
Development

No branches or pull requests

4 participants