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
Updating a Model with custom validation bug #13432
Comments
Also encountering this problem! Currently working around by using There's an old closed issue that seems related to this: #4447 |
There's a related open issue as well: #12666 |
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. 🙂 |
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. 🙂 |
Still an issue |
This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂 |
Still an issue |
This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂 |
Is there a way I can remove the "stale" label myself instead of repeatedly commenting that it's still an issue? |
This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂 |
Still relevant |
This doesn't sound like a bug. Custom validators will be called every time a model will be updated - including when calling bulk updates. Simply check whether the values your validator depend on are present before using them. cannotConnectToSelf() {
if (this.user1id === undefined || this.user2id === undefined) {
return;
}
if (this.user1id === this.user2id) {
throw new Error('Cannot connect to self.');
}
}, You'd have the same issue if you selected a subset of your model's attributes in |
@ephys, I don't understand how you can suggest simply not validating because what you're trying to validate isn't available, not because the values don't exist, but because Sequelize made an internal choice not to retrieve them? |
Because that would completely destroy your application's performance. It would require running a select during your bulk update, which could select a very large amount of data. If your constraint validation depends on more than one field, using check constraints would be much safer and efficient. If you really want to select data before updating it (which is going to have a large performance impact), you can also use the |
individualHooks indeed seems like it would do the trick. Thanks! |
@ephys, |
I can't verify this right now but: is it possible that it's running the validation twice? Once on the object that's passed to |
No, validation only runs once; for the object built from the passed properties. |
I see. That could have been intended but if Changing it in v6 could break some users but we're actively working on v7 right now so if someone wants to open a PR to change the behavior in v7 (main branch), we can include it before the first stable release |
This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂 |
@ephys can you add a label to this so the bot won't close this? |
Issue Creation Checklist
Bug Description
When trying to Model.update() a model with custom validation, an error is thrown as the custom validator does not have access to instance fields.
SSCCE
**Here is the link to the SSCCE for this issue: https://github.com/gsqrt2/sequelize-update-validation-error
What do you expect to happen?
In regard to the SSCCE:
should update Connections where approved===false to approved: true
What is actually happening?
The custom validator throws, as instance dataValues are undefined
Additional context
The SSCCE is extremely basic, just a dummy Connection with a custom validator that ensures that the two user ids are not equal. Commenting out the validation restores normal functionality.
Environment
Bug Report Checklist
How does this problem relate to dialects?
Would you be willing to resolve this issue by submitting a Pull Request?
The text was updated successfully, but these errors were encountered: