loopback 3.x-用户角色m:m通过角色Map

x33g5p2x  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(349)

我有一个自定义用户、角色和角色Map,名为adminuser、adminrole和adminrolemapping。我已经用数据填充了数据库,我正在使用mysql和loopback3.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" 我的设置有什么问题?我已经看过医生了。

j9per5c4

j9per5c41#

经过一番反复试验,重命名中间表中的关系名称,解决了以下问题:

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

相关问题