Feature-rich ORM for modern Node.js and TypeScript, it supports PostgreSQL (with JSON and JSONB support), MySQL, MariaDB, SQLite, MS SQL Server, Snowflake, Oracle DB (v6), DB2 and DB2 for IBM i.
I was trying to get all the users from the application with related data to each one: only the 'url' attribute of the selected avatar - there's a relationship between user and avatar of one-to-one. The url is a virtual field; it doesn't corresponds to a table column.
Well, what was happening is that the related data wasn't being returned in the result set, then i had to include as well another random attribute to make the result set provide a 'selectedAvatar' object containing 'url' as a property.
Ater that, i tested trying to get the result set with related data based on only virtual attributes - 'url' and 'otherVirtualField (just for the sake of a try!) -, it didn't returned the related data. If i inform just one virtual field to be included in the related data object, it doesn't includes it; if i inform one virtual and another that is not, then it includes; if i inform only virtuals, then it doesn't.
The conclusion i reached on this logic was the following: sequelize is not capable of including only virtual fields from related data in result set. That is: Include: [ { model: SomeModel, attributes: ['vrtualField', 'anotherVirtualField'] } ] don't work as expected, neither returns an error or something related.
Even when only virtual fields are included in 'attributes' property from 'include' array in the findOptions object of Mode.findAll method, they're returned in the related data of the result set.
What is actually happening?
When including only virtual fields as it is in the sscce, this is the returned set:
[ ] 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.
Issue Creation Checklist
Bug Description
I was trying to get all the users from the application with related data to each one: only the 'url' attribute of the selected avatar - there's a relationship between user and avatar of one-to-one. The url is a virtual field; it doesn't corresponds to a table column.
Well, what was happening is that the related data wasn't being returned in the result set, then i had to include as well another random attribute to make the result set provide a 'selectedAvatar' object containing 'url' as a property.
Ater that, i tested trying to get the result set with related data based on only virtual attributes - 'url' and 'otherVirtualField (just for the sake of a try!) -, it didn't returned the related data. If i inform just one virtual field to be included in the related data object, it doesn't includes it; if i inform one virtual and another that is not, then it includes; if i inform only virtuals, then it doesn't.
The conclusion i reached on this logic was the following: sequelize is not capable of including only virtual fields from related data in result set. That is:
Include: [ { model: SomeModel, attributes: ['vrtualField', 'anotherVirtualField'] } ]
don't work as expected, neither returns an error or something related.Reproducible Example
Here is the link to the SSCCE for this issue: https://github.com/augusto-dmh/sequelize-sscce-only-virtual-fields-result-set-bug
What do you expect to happen?
Even when only virtual fields are included in 'attributes' property from 'include' array in the findOptions object of Mode.findAll method, they're returned in the related data of the result set.
What is actually happening?
When including only virtual fields as it is in the sscce, this is the returned set:
When at least one non-virtual field is returned:
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.