亲爱的朋友们遇到了问题,因为包括联接表并抛出另一个表,然后我立即将其包括在内
如果我通过(提供商)访问(用户)表,(用户)表应返回提供商名称
如果错误地包含(用户)表,则意味着获得客户名称
但是我收到以下错误
ER_NONUNIQ_TABLE:不是唯一的表/别名:'Provider.User'
代码:
models.Order.findOne({
where: {
id: req.params.id
},attributes: ['orderStatus','id','serviceId','providerId','orderDescription',"orderScheduledDate",'userLat','userLng','createdAt'],
include: [
{
model: models.Provider,
attributes: ['id','userId'],
include : [{
model : models.User,
attributes: ['firstName','lastName','phoneNumber']
},{
model : models.User,
attributes: ['phoneNumber']
}]
}
]
})
答案 0 :(得分:0)
如果要两次包含相同的模型,则需要为关系/关联分配别名:
Provider.belongsTo/haveMany/any...(User, {as: 'ProviderUser'}); //<------ HERE
Provider.belongsTo/haveMany/any...(User, {as: 'User'}); //<------ HERE
include: [{
model: models.Provider,
attributes: ['id', 'userId'],
include: [{
model: models.User,
as : 'User' //<---------- HERE
attributes: ['firstName', 'lastName', 'phoneNumber']
}, {
model: models.User,
as : 'ProviderUser' //<---------- HERE
attributes: ['phoneNumber']
}]
}]