sequelize-typescript: Alias cannot be inferred
Given the following models, each of the associations work fine in isolation but not if used together. I’m getting:
Error: Alias cannot be inferred: “Estimate” has multiple relations with “Employee”
What am I doing wrong?
@Table()
export default class Estimate extends Model<Estimate> {
// Fields....
@ForeignKey(() => Employee) @Column estimatorId: number;
@BelongsTo(() => Employee) estimator: Employee;
@ForeignKey(() => Employee) @Column foremanId: number;
@BelongsTo(() => Employee) foreman: Employee;
@BelongsToMany(() => Employee, () => Crew) crew: Employee[];
}
@Table()
export default class Employee extends Model<Employee> {
// Fields....
@HasMany(() => Estimate, 'estimatorId') estimatesAsEstimator: Estimate[];
@HasMany(() => Estimate, 'foremanId') estimatesAsForeman: Estimate[];
@BelongsToMany(() => Estimate, () => Crew) estimatesAsCrew: Estimate[];
}
@Table()
export default class Crew extends Model<Crew> {
@ForeignKey(() => Employee) @Column employeeId: number;
@ForeignKey(() => Estimate) @Column estimateId: number;
}
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 22 (12 by maintainers)
@doovers Does the error happen while initializing these models or when trying to retrieve data? If it happens when retrieving data, you also need to define what exact
include
do you want to use. Therefor you have to setas
explicitly like:@RobinBuschmann Apologies for the late reply! I had another look at this and it happens when I call
create
on a model thatBelongsTo
a model withinclude: [{ all: true }]
Logging
include
at/node_modules/sequelize/lib/model.js:465:29
yields:Note that the model attribute is missing where
all: true
but in the parent it is present.