Thanks to visit codestin.com
Credit goes to github.com

Skip to content

When include is used for association query, after json escape is performed on the returned object, attributes declared in DataTypes.VIRTUAL cannot be obtained by default. #18059

@yuntian001

Description

@yuntian001

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

I created a Virtual attributes url and declared DataTypes.VIRTUAL

import { DataTypes } from '@sequelize/core';
import {  PrimaryKey} from '@sequelize/core/decorators-legacy';

class File extends Model {
  @Attribute(DataTypes.STRING)
  @PrimaryKey
  @NotNull
  id: string;

  @Attribute(DataTypes.STRING)
  @NotNull
  declare name: string;

  @Attribute(DataTypes.STRING)
  @NotNull
  declare path: string;

  @Attribute(DataTypes.VIRTUAL(DataTypes.STRING, ['name', 'path']))
  get url(): string {
    return this.path+'/'+this.name
  }
}

When I query it and convert it to json and return to the front, the return value is with the url attribute. That's good.

const files= await File.findAll();
console.log(JSON.stringify(files));

However, when it is loaded by association and converted to json and returned to the front, it cannot get the url.

import { DataTypes } from '@sequelize/core';
import { Attribute,  BelongsTo } from '@sequelize/core/decorators-legacy';
class User extends Model {
  @Attribute({ type: DataTypes.STRING(20),})
  avatarFileId: string;

  @BelongsTo(() => File, /* foreign key */ 'avatarFileId')
  avatar?: NonAttribute<File>
}
const users= await User .findAll({include:['avatar']});
console.log(JSON.stringify(users));

Reproducible Example

Here is the link to the SSCCE for this issue:

I created a Virtual attributes url and declared DataTypes.VIRTUAL

import { DataTypes } from '@sequelize/core';
import {  PrimaryKey} from '@sequelize/core/decorators-legacy';

class File extends Model {
  @Attribute(DataTypes.STRING)
  @PrimaryKey
  @NotNull
  id: string;

  @Attribute(DataTypes.STRING)
  @NotNull
  declare name: string;

  @Attribute(DataTypes.STRING)
  @NotNull
  declare path: string;

  @Attribute(DataTypes.VIRTUAL(DataTypes.STRING, ['name', 'path']))
  get url(): string {
    return this.path+'/'+this.name
  }
}

When I query it and convert it to json and return to the front, the return value is with the url attribute. That's good.

const files= await File.findAll();
console.log(JSON.stringify(files));

However, when it is loaded by association and converted to json and returned to the front, it cannot get the url.

import { DataTypes } from '@sequelize/core';
import { Attribute,  BelongsTo } from '@sequelize/core/decorators-legacy';
class User extends Model {
  @Attribute({ type: DataTypes.STRING(20),})
  avatarFileId: string;

  @BelongsTo(() => File, /* foreign key */ 'avatarFileId')
  avatar?: NonAttribute<File>
}
const users= await User .findAll({include:['avatar']});
console.log(JSON.stringify(users));

What do you expect to happen?

when it is loaded by association and converted to json and returned to the front, it can get the url.

What is actually happening?

when it is loaded by association and converted to json and returned to the front, it cannot get the url.

Environment

  • Sequelize version: @sequelize/[email protected]
  • Node.js version: v22.14.0
  • If TypeScript related: TypeScript version: 5.9.3
  • Database & Version: PostgreSQL 16.1
  • Connector library & Version: @sequelize/postgres 7.0.0-alpha.47

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

    pending-approvalBug reports that have not been verified yet, or feature requests that have not been accepted yet

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions