sequelize-typescript: Problems with multiple relationships

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!

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

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

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

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! 😄

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! 😄