Skip to content

Virtual attribute set function can't always get other attributes #348

Open
@JoakimFFCG

Description

@JoakimFFCG

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

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

// Works fine
user.firstName = 'Firstname';
user.setNote = 'Yet other note';
user = await user.save();
console.log(user.note);       // It's "Firstname: Yet other note"

// Doesn't 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"

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

  • Sequelize version: 6.25.6 & 7.0.0-alpha.10
  • Node.js version: v18.12.1
  • If TypeScript related: TypeScript version: Not typescript related
  • Database & Version: Shouldn't be database related
  • Connector library & Version: Shouldn't be database related

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 will need guidance.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions