如何使用續集制作此代碼?
我使用了include,但它帶來了所有的用戶資料,但我只需要名字
select comments.comment, users.name from comments inner join users on comments.userId = users.id
我的代碼:
Comments.findAll({
where: {
projectId: id
},
include: User
}).then(response =>{
res.json({comments: response})
})
uj5u.com熱心網友回復:
如果您只想減少User模型屬性,則只需在attributes選項中指明所需的屬性:
Comments.findAll({
where: {
projectId: id
},
include: [{
model: User,
attributes: ['name']
}]
}).then(response =>{
res.json({comments: response})
})
如果要將用戶名添加為與所有Comments欄位處于同一級別的字串屬性,只需在具有所需別名attributes的Comments模型選項中指出它:
Comments.findAll({
where: {
projectId: id
},
attributes: [[Sequelize.col('Users.name'), 'userName']],
include: [{
model: User,
attributes: []
}]
}).then(response =>{
res.json({comments: response})
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/364991.html
