Having trouble implementing many-to-many self-assiciations
JaRoMasterDev opened this issue · comments
JaRoMaster commented
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;
}
Cesar Iduarte commented
I can reproduced this with @BelongsTo associations too.