-
Notifications
You must be signed in to change notification settings - Fork 283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sequelize get({ plain: true })
or toJSON()
doesn't work with associations
#940
Comments
Found temporary solution - use Second.findByPk(1, { include: [First], raw: true, first: true }); |
are there any updates?( |
I either use @Table({ tableName: 'First' })
export class First extends Model<IFirst> implements IFirst {
@PrimaryKey
@AutoIncrement
@Column
public id: number;
@Column
public title: string;
}
export class Second extends Model<ISecond> implements ISecond {
@PrimaryKey
@AutoIncrement
@Column
public id: number;
@Column
public title: string;
@ForeignKey(() => First)
@Column
public firstId: number;
@BelongsTo(() => First)
public first: IFirst;
toJSON(): object {
return { ...this.get(), first: this.first }
}
}
export interface IFirst { id: string; title: string }
export interface ISecond { id: string; title: string }
async function test(): Pormise<void> {
const result = await Second.findByPk(1, { include: [First] });
console.log(result.toJSON().first);
} |
after dozens of different "hacks" i get to next - ORMs in JS r so inmature :( |
you can run a raw query if you cannot get exactly what you want: https://sequelize.org/docs/v6/core-concepts/raw-queries/ |
Hi!
Thanks for your effort and this lib!
Issue
I've been using sequelize-typescript for some time and noticed, that associations are not added to
dataValues
of native Sequelize object when fetching any records.Because of this, after calling
Sequelize.get({ plain: true })
orSequelize.toJSON()
- there is no association data in resulting object.Versions
Issue type
Actual behavior
Expected behavior
Steps to reproduce
a bit similar to #605
The text was updated successfully, but these errors were encountered: