使用nodejs(10.15.0)、sequelize (4.42.0)和MySQL,尝试从连接查询的结果中删除表路径。
Table1
.findAll({
attributes: ['id', 'name', 'other'],
include: [{
attributes: ['code_id'],
model: Table2,
nested: false,
required: true,
}],
raw: true,
})
查询结果
[
{
"id": 1,
"name": "stuff",
"other": true,
"table2.code_id": 1,
}
]
期待发生
[
{
"id": 1,
"name": "stuff",
"other": true,
"code_id": 1,
}
]
最佳答案
删除 raw: true,
- 它阻止 Sequelize 将结果解析为对象。如果您不想使用模型实例,则需要为结果编写自己的解析器。
请注意,它将解析为以下结构(“table2”将是一个属性):
[
{
"id": 1,
"name": "stuff",
"other": true,
"table2": {
"code_id": 1,
}
}
]
或者,您可以为子行添加别名,但请注意,除非您创建映射到它的 VIRTUAL 字段,否则它只会进入 dataValues
。
Table1
.findAll({
attributes: [
'id', 'name', 'other',
[sequelize.col('table2.code_id'), 'code_id'], // aliased here
],
include: [{
attributes: [],
model: Table2,
required: true,
}],
raw: true,
})
关于mysql - 删除连接查询结果中的前缀表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53958945/