我有一个自定义用户,角色和角色映射,分别名为AdminUser,AdminRole和AdminRoleMapping。我已经用数据填充数据库,并且正在使用Mysql和Loopback 3.19。我的模型配置是:
{
"name": "AdminRole",
"plural": "adminRoles",
...
"base": "Role",
"relations": {
"users": {
"type": "hasMany",
"model": "AdminUser",
"foreignKey": "principalId",
"through": "AdminRoleMapping"
}
},
...
}
{
"name": "AdminUser",
"plural": "adminUsers",
"base": "User",
...
"roles": {
"type": "hasMany",
"model": "AdminRole",
"foreignKey": "principalId",
"through": "AdminRoleMapping"
}
},
...
}
{
"name": "AdminRoleMapping",
"description": "Map principals to roles",
"plural": "adminRoleMappings",
"base": "RoleMapping",
....
"relations": {
"principal": {
"type": "belongsTo",
"model": "AdminUser",
"foreignKey": "principalId"
},
"roles": {
"type": "belongsTo",
"model": "AdminRole",
"foreignKey": "roleId"
}
},
....
}
当我尝试访问GET /adminUsers/{id}/roles
时,我收到状态错误500
"message": "Relation \"adminRole\" is not defined for AdminRoleMapping model"
我的设置有什么问题?我已经关注了文档。
答案 0 :(得分:0)
经过反复试验后,在中间表中重命名关系名称,解决了该问题:
{
"name": "AdminRoleMapping",
"description": "Map principals to roles",
"plural": "adminRoleMappings",
"base": "RoleMapping",
...
"relations": {
"adminUser": {
"type": "belongsTo",
"model": "AdminUser",
"foreignKey": "principalId"
},
"adminRole": {
"type": "belongsTo",
"model": "AdminRole",
"foreignKey": "roleId"
}
},
...
}