-
-
Notifications
You must be signed in to change notification settings - Fork 155
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
Virtual attribute set function can't always get other attributes #348
Comments
I'm sorry to say none of these are bugs. It's just how setters work user.setNote = 'Note!';
user.firstName = 'FN';
user = await user.save();
console.log(user.note); // It's "Firstname: Note!"
console.log(user.firstName); // It's "FN" In this example, when the setNote setter is called, firstName hasn't been updated yet, so the setter can only access the old value of firstName. You'd have the same result with plain JavaScript setters (which these basically are) await User.update({
setNote: 'My note'
}, {
where: { id: user.id }
});
user = await User.findOne();
console.log(user.note); // It's "undefined: My note"
console.log(user.firstName); // It's "Another" This is normal too. // Doesn't work
await User.update({
setNote: 'My note 2',
firstName: user.firstName,
}, {
where: { id: user.id }
}); This one is weirder but also "normal". Best we could do here is assign virtual properties after everything else. But you'd still have to worry about the execution order of the setters themselves. Again all of these issues aren't really sequelize issues. They're just the sort of issues that happen when you try to access other properties in a setter (including plain JavaScript setters). You're going to have unintuitive behaviors. Final thoughts: accessing a property like this in your setter is a bad idea. You should probably avoid it completely, and explore other options to achieve your goal. |
Well, then the documentation probably shouldn't do it:
If setters really are supposed to work like just plain JavaScript setters, I really think that has to be explained in the documentation. |
I agree, that must be changed & warned about. It's too bug prone to let people know about it (even worse that it's including an example of it), I'll try to include it in our rewrite |
Issue Creation Checklist
Bug Description
In a set function on a virtual attribute you can't always get other attributes.
Depending on how the set is triggered the specifics differ. But for example, with Model.update() the set doesn't work properly when the attributes it uses aren't in the 'values' argument (and only if the attributes are specified before the virtual attribute).
See the examples for some specific cases.
Reproducible Example
Here is the link to the SSCCE for this issue: https://github.com/sequelize/sequelize-sscce
Also, a similar example not based on sequelize-sscce: https://github.com/JoakimFFCG/sequelize-demo
(I first did the example without sequelize-sscce, so might as well link that too. Also, it runs all the test cases, not stopping at the first one that fails.)
Example code from https://github.com/JoakimFFCG/sequelize-demo/blob/main/index.js
What do you expect to happen?
In a set function on a virtual attribute you should always be able to get other attributes.
What is actually happening?
In a set function on a virtual attribute you can't always get other attributes (they are undefined). See the code examples for specifics.
Environment
Would you be willing to resolve this issue by submitting a Pull Request?
Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.
The text was updated successfully, but these errors were encountered: