eggjs / eggjs/egg

关于连表查询嵌套太多的问题

Open
#4,620 0 comments 0 reactions 0 assignees View on GitHub
Inactive
Dominant language
TypeScript
Stars
19k
Forks
1.8k
PR merge metrics
No merged PRs in 30d

Description

### 超过三级嵌套的时候,想合利用Sequelize.col()合并字段
#### 正常合并字段
``` js
await this.ctx.model.Student.findAll({
attributes: [[this.app.Sequelize.col('subject.name'), 'subjectName']],
include: [
{
model: this.ctx.model.Subject,
as: 'subject',
attributes[]
}
]
})
```
预期结果
``` json
[
{
"id": "1",
"name": "student1",
"subjectName": "数学"
},
{
"id": "1",
"name": "student1",
"subjectName": "语文"
}
]
```
#### 如果加一级成绩,并且吧成绩合并到学科这一级
``` js
await this.ctx.model.Student.findAll({
include: [
{
model: this.ctx.model.Subject,
as: 'subject',
attributes: [[this.app.Sequelize.col('subject->studentScores.score'), 'subjectScore']],
include: [
{
model: this.ctx.model.StudentScores,
as: 'studentScores',
}
]
}
]
})
```
结果会报错

`Unknown column 'subject.subject->studentScores.score' in 'field list'`
框架会默认吧前面的表名加上!
如果是四级
`Unknown column 'subject->studentScores.${Sequelize.col()}' in 'field list'`

### 这到底是一个缺陷呢还是我没有找到方法

## 相关环境信息
- **操作系统**:windows
- **Node 版本**:v12.13.0
- **Egg 版本**:2.15.1

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.