-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
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.