这是我在项目中完成的代码和结果。 我想获得包含模型的结果与主模型相同的结果。下面的代码是我所做的。
序列化查询:
User.findAll({
include: [{
model: Position,
attributes: ['POSITION_NAME'],
}
]
}).then(user => {
res.json({
data: user
})
}).catch(error => {
console.log(error)
})
我得到的结果
[
{
"ID": 2,
"NAME": "AAA",
"LAST_NAME": "BBB",
"Position": {
"POSITION_NAME": "ວິຊາການສັນຍາຈ້າງ"
}
}
]
我想要的结果:
[
{
"ID": 2,
"NAME": "AAA",
"LAST_NAME": "BBB",
"POSITION_NAME": "ວິຊາການສັນຍາຈ້າງ"
}
]
最佳答案
尝试这样。
User.findAll({
attributes: ["ID", "NAME", ["Position.POSITION_NAME", "POSITION_NAME"]],
include: [{
model: Position,
attributes: [],
}
],
raw:true
}).then(user => {
res.json({
data: user
})
}).catch(error => {
console.log(error)
})
关于mysql - 如何在Sequelize中从主模型同一级别的包含模型返回结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62507000/