Getters not being called on find or get({plain: true}) neither
See original GitHub issueWhat are you doing?
I’ve got a model with a virtual method called payULink()
that produces an URL using the id field of the model. On Sequelize 4.44.0, calling findAll()
or get()
on an instance calls the methods that produce this virtual property correctly, but on sequelize 5.8.5 it does not.
let purchaseOrder = sequelize.define('purchaseOrder', attributes, {
getterMethods: {
payULink() {
return somethingMagic(this.id);
}
}
});
To Reproduce Steps to reproduce the behavior:
- Define a model with getters.
- Run findAll() on the model or get() on an instance.
- See error.
What do you expect to happen?
I was expecting to receive the object with the virtual properties obtained from calling the methods.
What is actually happening?
The object only contains the properties that are not virtual.
Environment
Dialect:
- mysql
- postgres
- sqlite
- mssql
- any Dialect library version: mysql2 1.6.5 Database version: 5.7.18 Sequelize version: 5.8.5 Node Version: 10.15.3 OS: macOS Mojave If TypeScript related: TypeScript version: XXX Tested with latest release:
- No
- Yes, specify that version: 5.8.3
Issue Analytics
- State:
- Created 4 years ago
- Comments:16 (6 by maintainers)
Top Results From Across the Web
Sequelize getters do not run on model.findall( { raw:true } )
Right, that is what the description says. getter should not be called when raw: true is applied. So if you want the getter...
Read more >getter - JavaScript - MDN Web Docs - Mozilla
The get syntax binds an object property to a function that will be called when that property is looked up. It can also...
Read more >Getters, Setters & Virtuals - Sequelize
Sequelize allows you to define custom getters and setters for the attributes of your models.
Read more >Node.js v19.3.0 Documentation
stack <Object> A stack trace of the function. The arrays contains information about the expected and actual number of calls of the functions...
Read more >Prototype methods, objects without __proto__
This call makes a truly exact copy of obj , including all properties: enumerable and non-enumerable, data properties and setters/getters ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
They recommend making them virtual
But the breaking change/bug is that … if your query has an
attributes
array… the virtual field wont be returned unless specifically requested… see my comment above.Its worth noting that after making them virtual fields - they will now also show up in the model instances list of fields (
model.rawAttributes
). i don’t know if you have any code that inspects the fields on your models… but I had to modify some of our rest api to account for the virtual fields showing up as a field. Mainly because our rest api allows you tosort
a query if you pass it a valid field… and trying to sort by a virtual field blows up… (which might be another bug)@papb we have > 50 models (with many
getterMethods
). This was not documented at all as a breaking change and I feel it should either be fixed or documented. I am curious whygetterMethods
are being deprecated? What is the best practice instead of them? I know you mentioned usingVIRTUAL
columns but how do you convert to them from something like this: