Sequelize-typescript: Problems with multiple relationships

Created on 12 Sep 2018  路  15Comments  路  Source: RobinBuschmann/sequelize-typescript

Hi!, in my project I'm getting the following error:

UnhandledPromiseRejectionWarning: Error: Alias cannot be inferred: "ProjectRecurrence" has multiple relations with "Projects"

My code is:

export default class ProjectRecurrence extends Model<ProjectRecurrence> {

    ...

    @ForeignKey(() => Projects)
    @IsInt
    @Column
    project_id: number;

    @BelongsTo(() => Projects, 'project_id')
    projectIdObject: Projects;

    ...

    @HasMany(() => Projects, 'project_recurrence_id')
    projects: Projects[];

}

is a bug? thank you!

Most helpful comment

Hey @Dragons0458, your setup is totally fine. The issue is the query itself. You have to tell sequelize-typescript which relation to ProjectRecurrence you want to use:

Projects.findAll({
    include: [{
        model: ProjectRecurrence,
        as: 'projectRecurrenceIdObject' // <---
    }]
})

Hope this helps. Feel free to close this issue if my answer solves your problem

All 15 comments

Hey @Dragons0458, can you provide the query which causes this error?

Hello! is when starting the server

Hi,

i have a similar error... I'm trying to attach a user as an author and as an assignee of a task.

Tried two approaches:

User.ts

export default class User extends Model<User> {
    ...

    @ForeignKey(() => User)
    @Column
    authorId: string;

    @BelongsTo(() => User, "authorId")
    author: User;


    @ForeignKey(() => User)
    @Column
    assigneeId: string;

    @BelongsTo(() => User, "assigneeId")
    assignee: User;
    ...
}

and

export default class User extends Model<User> {
    ...

    @BelongsTo(() => User, "authorId")
    author: User;

    @BelongsTo(() => User, "assigneeId")
    assignee: User;
    ...
}

combined with this (and without any mention in task.ts):
task.ts

export default class Task extends Model<Task> {

    @HasMany(() => Issue, "authorId")
    authoredIssues: Issue[];

    @HasMany(() => Issue, "assigneeId")
    assignedIssues: Issue[];
}

The result was always the same:

/Users/***/node_modules/sequelize-typescript/lib/services/models.js:258
                throw new Error("Alias cannot be inferred: \"" + source.name + "\" has multiple " +
                ^

Error: Alias cannot be inferred: "Issue" has multiple relations with "User"
    at inferAliasForInclude

Any help here? Its crashing on startup, just like the error of @Dragons0458. As soon as i call this:

import { Sequelize } from "sequelize-typescript";
import { Config } from "./config";

const DBSequelize = new Sequelize({
  host: Config.db.host,
  dialect: Config.db.dialect,
  operatorsAliases: Sequelize.Op as any,
  database: Config.db.name,
  username: Config.db.user,
  password: Config.db.password,
  modelPaths: [__dirname + "/associations", __dirname + "/models"],
  logging: false,
});

Got it to work... I missed my defined scope to have an as in the includes. Now it works like a charm, maybe its the same problem @Dragons0458 ?

@Dragons0458 The foreign key decorator is just a helper to infer the foreign keys automatically. If you have multiple relations of the same model (which is Project in your case) the foreign keys need to be set explicitly - like you did. So no need to the foreign key annodation - Just omit it and it should work!

@Dragons0458 Do you still have any issues with this? Can we close this issue then?

My apologies for not answering before, @mk4arts it's not my same problem unfortunately, in the end I could not solve it at all and I commented one of the attributes of the code (the one I was not going to use), but if I needed it if I did not know I would (maybe a native query?), thank you very much!

ok, ok, I just tried with version 0.6.6 of this library and I do not get the error, sorry for not looking before, thank you very much for the solution to this! :D

Sorry to reopen the thread after so much time, but I have returned to get the same error and I do not know why, the models and classes are the following:

export default class ProjectRecurrence extends Model<ProjectRecurrence> {

    ...

    @ForeignKey(() => Projects)
    @IsInt
    @Column
    project_id: number;

    @BelongsTo(() => Projects, 'project_id')
    projectIdObject: Projects;

    ...

    @HasMany(() => Projects, 'project_recurrence_id')
    projects: Projects[];

}

and

export default class Projects extends Model<Projects> {

    ...

    @ForeignKey(() => ProjectRecurrence)
    @IsInt
    @Column
    project_recurrence_id: number;

    @BelongsTo(() => ProjectRecurrence, 'project_recurrence_id')
    projectRecurrenceIdObject: ProjectRecurrence;

    ...

    @HasMany(() => ProjectRecurrence, 'project_id')
    projectsRecurrences: ProjectRecurrence[];

   ...

}

and this is the model:

imagen

These are the versions I am using:

sequelize-typescript: 1.0.0-beta.3
sequelize: 5.8.7
nodejs: 10.15.3

any solution?

Thank you very much! :D

@Dragons0458 can you provide an example repo, which reproduces the issue?

Hi! this is the repository: https://github.com/Dragons0458/AliasCannotBeInferred thank you!

Hey @Dragons0458, your setup is totally fine. The issue is the query itself. You have to tell sequelize-typescript which relation to ProjectRecurrence you want to use:

Projects.findAll({
    include: [{
        model: ProjectRecurrence,
        as: 'projectRecurrenceIdObject' // <---
    }]
})

Hope this helps. Feel free to close this issue if my answer solves your problem

Hello! My apologies for the delay in responding.

Thank you for your help! :D

The query that shows me works for me, but when I do the opposite it does not work, what could it be? Thank you!

ProjectRecurrence.findAll({
    include: [{
        model: Projects,
        as: 'projectIdObject'
    }]
}).then(console.log).catch(console.error);

@Dragons0458 Do you receive the same error? I don't get any - It's working very well for me.

I just checked and if it works correctly, sorry for the inconvenience, for now it was all good, thank you very much for your help! I will close the issue: D

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nandox5 picture nandox5  路  3Comments

fareshan picture fareshan  路  3Comments

oscarcalvo picture oscarcalvo  路  3Comments

fareshan picture fareshan  路  4Comments

lilling picture lilling  路  4Comments