我有一个自定义的用户,角色和角色映射,分别名为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"

我的设置有什么问题?我已经关注了文档。

最佳答案

经过一番尝试和错误后,在中间表中重命名了关系名称,从而解决了该问题:

{
  "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"
    }
  },
  ...
}

关于javascript - 回送3.x-通过RoleMapping的用户 Angular 色M:M关系,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52446440/

10-12 15:46