Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having trouble implementing many-to-many self-assiciations #1668

Open
1 of 2 tasks
JaRoMasterDev opened this issue Jun 5, 2023 · 1 comment
Open
1 of 2 tasks

Having trouble implementing many-to-many self-assiciations #1668

JaRoMasterDev opened this issue Jun 5, 2023 · 1 comment

Comments

@JaRoMasterDev
Copy link

JaRoMasterDev commented Jun 5, 2023

Issue

I am trying to implement a many-to-many relation between two entities inside the same relation.

Versions

  • sequelize: ^6.31.1
  • sequelize-typescript: ^2.1.5
  • typescript: ^5.0.4

Issue type

  • bug report
  • feature request

Actual behavior

When adding a scope to load the entities as in the sequelize-typescript-example, I am getting the following error:
Error: Alias cannot be inferred: "User" has multiple relations with "User"

Expected behavior

Being able to access the followers and follows property of a user element.

Steps to reproduce

Adding the Models as below and trying to start the app.

Related code

// User model
@Scopes(() => ({
  followers: {
    include: [{ model: User, through: { attributes: [] } }],
  },
}))
@Table({ updatedAt: false })
export class User extends Model<Partial<User>> {
  @Unique
  @Column
  name?: string;

  @Unique
  @Column
  email!: string;

  @Column
  hashedPw!: string;

  @BelongsToMany(() => User, () => Follows, "userId", "followsId")
  followers?: User[];

  @BelongsToMany(() => User, () => Follows, "followsId", "userId")
  follows?: User[];

  @CreatedAt
  @Column
  createdAt!: Date;

  @UpdatedAt
  @Column
  updatedAt!: Date;
}

//Follows model
@Table({ updatedAt: false })
export class Follows extends Model<Partial<Follows>> {
  @ForeignKey(() => User)
  @Column
  userId!: number;

  @ForeignKey(() => User)
  @Column
  followsId!: number;
}
@suiz0
Copy link

suiz0 commented Sep 30, 2023

I can reproduced this with @BelongsTo associations too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants