codestates / codestates/art-ground

[Error Handling Log] sequelize JOIN issue/이동욱

Open
#87 0 comments 0 reactions 0 assignees View on GitHub
bug Error Handling Log server
Dominant language
JavaScript
Stars
7
Forks
9
PR merge metrics
No merged PRs in 30d

Description

### **어떤 에러인가요?**

- 모델간 조인 시 별칭 에러 발생

### **에러 메시지**

``` X is associated to exhibition using an alias. You must use the 'as' keyword to specify the alias within your include statement.
$ 터미널의 에러 코드를 여기 넣어주세요.
```

### **에러 핸들링 방법**

- 핸들링 방법에 대해서 간단하게 기록해보세요.

각 모델의 관계 설정 코드에 as 로 별칭이 설정되어있다 . (sequelize-auto 에서 지정한듯)

ex)
```js

exhibition.associate = function (models) {
exhibition.hasMany(models.comments, {
as: "comments", //얘
foreignKey: "exhibition_id",
});
exhibition.hasMany(models.images, {
as: "images", //얘
foreignKey: "exhibition_id",
});
exhibition.hasMany(models.likes, {
as: "likes", //얘
foreignKey: "exhibition_id",
});
exhibition.belongsTo(models.users, {
as: "author", //얘
foreignKey: "author_id",
});
};
```

원래는

```js
const result = await exhibitionModel.findAll({include: images})

```
이렇게 하면 해결 되지만 별칭이 설정되어 있는 경우

``js
const result = await exhibitionModel.findAll({
include: [
{
model: images,
as: "images",
},
],
});
```
이렇게 따로 별칭을 지정해 줘야 한다

### **에러 핸들링을 위해 참고한 레퍼런스 링크**

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.