I’m working on a NodeJs + Sequelize + TypeScript app. I use sequelize-typescript lib if this matters.
I have those models :
@Table({
timestamps: true,
paranoid: true,
})
class Ingredient
extends Model
{
@PrimaryKey
@AutoIncrement
@Column
public id!: number;
@Column
public name!: string;
@ForeignKey(() => Department)
@Column
public departmentId: number;
@BelongsTo(() => Department)
department: Department;
}
export default Ingredient;
@Table({
timestamps: true,
paranoid: true,
})
class Department
extends Model<DepartmentAttributes, DepartmentInput>
{
@PrimaryKey
@AutoIncrement
@Column
public id!: number;
@Column
public name!: string;
}
export default Department;
When I create a new Ingredient I would like that method returns the created Ingredient, with Department included.
I tried :
const ing = await Ingredient.create(payload, {
include: [Department]
});
But it doesn’t work, I don’t get the Department included in ing.
I also need it to be included in findAll, and this is working :
return await Ingredient.findAll({
where: {
...(filters?.isDeleted && { deletedAt: { [Op.not]: null } }),
},
...((filters?.isDeleted || filters?.includeDeleted) && { paranoid: true }),
include: [Department],
});
Any idea about why include doesn’t work with create() method ?