环回 3.x - 通过 RoleMapping 的用户角色 M:M 关系

Loopback 3.x - User Role M:M relation though RoleMapping

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

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

经过反复试验,重命名中间关系名称 table,解决了问题:

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