codestates / codestates/Naengttatouille

[Error-handling] SequelizeAssociationError: Aliased associations must have unique aliases.

Open
#49 0 comments 0 reactions 2 assignees Claimed by @js5787 View on GitHub
Completed error
Dominant language
JavaScript
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

### 어떤 에러인가요?
* 에러 메시지
```
SequelizeAssociationError: You have used the alias User_ingredient in two separate associations. Aliased associations must have unique aliases.
```
### 에러 핸들링 방법

* (에러) 초기 생성 시 모델을 중복으로 생성하였다는 에러 내용으로 판단됨.
* (원인) 모델에서 관계 설정 시 중복으로 작성
[구조] User : Ingredient = N : M, Join 테이블 : User_ingredient
관계 설정 시 조인테이블을 통한(through 옵션) N:M 관계를 설정하면 되는데, 1:N 관계를 각 해당 테이블에서 중복으로 작성함
(Turns out the problem is because I registering the model twice at initial setup on Sequelize. So removing duplicates model fix the core problems for me.)

* (해결)
* models/user.js
```
static associate(models) {
// define association here
this.belongsToMany(models.Ingredient, {
through: 'User_ingredient',
foreignKey: 'user_id',
});
}
```

* models/user_ingredient.js
```
static associate(models) {
this.belongsTo(models.User, {
foreignKey: 'id',
});
this.belongsTo(models.Ingredient, {
foreignKey: 'id',
});
}
```

* models/ingredient.js
```
static associate(models) {
// define association here
this.belongsToMany(models.User, {
through: 'User_ingredient',
foreignKey: 'ingredient_id',
});
}
```

### 에러 핸들링을 위해 참고한 레퍼런스 링크
[링크]() #

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.