sequelize / sequelize

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.
https://sequelize.org/
MIT License
29.25k stars 4.25k forks source link

sequelize not includes only virtual fields attributes from related data in result set #17353

Open augusto-dmh opened 1 month ago

augusto-dmh commented 1 month ago

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:

[
  User {
    dataValues: { id: 1, nickname: 'User1', selectedAvatarId: 1 },
    _previousDataValues: { id: 1, nickname: 'User1', selectedAvatarId: 1 },
    uniqno: 1,
    _changed: Set(0) {},
    _options: {
      isNewRecord: false,
      _schema: null,
      _schemaDelimiter: '',
      include: [Array],
      includeNames: [Array],
      includeMap: [Object],
      includeValidated: true,
      attributes: [Array],
      raw: true
    },
    isNewRecord: false
  }
]

When at least one non-virtual field is returned:

[
  User {
    dataValues: {
      id: 1,
      nickname: 'User1',
      selectedAvatarId: 1,
      selectedAvatar: [Avatar]
    },
    _previousDataValues: {
      id: 1,
      nickname: 'User1',
      selectedAvatarId: 1,
      selectedAvatar: [Avatar]
    },
    uniqno: 1,
    _changed: Set(0) {},
    _options: {
      isNewRecord: false,
      _schema: null,
      _schemaDelimiter: '',
      include: [Array],
      includeNames: [Array],
      includeMap: [Object],
      includeValidated: true,
      attributes: [Array],
      raw: true
    },
    isNewRecord: false,
    selectedAvatar: Avatar {
      dataValues: [Object],
      _previousDataValues: [Object],
      uniqno: 1,
      _changed: Set(0) {},
      _options: [Object],
      isNewRecord: false
    }
  }
]

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.

rafatosta commented 1 week ago

Hi, I'm experiencing the same problem. Do we have a solution?