Virtual attribute set function can't always get other attributes

Open
#348 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
30/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript, node.js
Domain
api, backend

Research direction

Start by running the linked sequelize-sscce reproduction and comparing it with the example in index.js from sequelize-demo. Trace the virtual attribute setter during the shown assignment orders and Model.update() cases; done means the setter can read the other attributes consistently and the reproduced cases pass.

Written by the indexing model from the issue text.

Description

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.

Dominant language
HTML
Stars
30
Forks
170
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sequelize/website

All issues in sequelize/website

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.