question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Alias cannot be inferred

See original GitHub issue

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;

}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:22 (12 by maintainers)

github_iconTop GitHub Comments

64reactions
RobinBuschmanncommented, Mar 14, 2018

@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 set as explicitly like:

Employee.findAll({
  include: [{model: Estimate, as: 'estimatesAsEstimator'}]
})
1reaction
dooverscommented, Apr 18, 2018

@RobinBuschmann Apologies for the late reply! I had another look at this and it happens when I call create on a model that BelongsTo a model with include: [{ all: true }]

Logging include at /node_modules/sequelize/lib/model.js:465:29 yields:

  static _validateIncludedElement(include, tableNames, options) {
    console.log(include);
    tableNames[include.model.getTableName()] = true; // Fails here
{
    "all": true,
    "parent": {
        "model": "Employee",
        "as": "estimator",
        "association": "estimator",
	...
    }
}

Note that the model attribute is missing where all: true but in the parent it is present.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Alias cannot be inferred · Issue #327 - GitHub
I'm getting: Error: Alias cannot be inferred: "Estimate" has multiple relations with "Employee" What am I doing wrong? @Table() export ...
Read more >
Alias for the associated models using Sequelize typescript
Is there a way to add an alias for the associated model? Like instead of Name in the Company I would want it...
Read more >
Type Aliases vs Interfaces in TypeScript - DEV Community ‍ ‍
Like variables, you can't declare the same type alias more than once. Aliases are also never inferred by TypeScript, you have to explicitly ......
Read more >
Common issues and solutions - mypy 0.991 documentation
The reason is that if the type of a is unknown, the type of a.split() is also unknown, so it is inferred as...
Read more >
Aliases | Elasticsearch Guide [8.5] | Elastic
An alias cannot point to both data streams and indices. You also cannot add a data stream's backing index to an index alias....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found