在 belongsTo 关系中获取完整的 json 结构

Get full json struct in a belongsTo relation

我正在尝试通过创建 belongsTo 关系在 Loopback 中设置基本模型关系。

我有两个这样定义的模型:

Contract.json

{
  "name": "Contract",
  "base": "PersistedModel",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mssql": {
    "schema": "dbo",
    "table": "Contract"
  },
  "properties": {
    "contractid": {
       // some property stuff
    },
  "validations": [],
  "relations": {
    "employee": {
      "type": "belongsTo",
      "model": "Employee",
      "foreignKey": ""
    }
  },
  "acls": [],
  "methods": []
}

Employee.json

{
  "name": "Employee",
  "base": "PersistedModel",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mssql": {
    "schema": "dbo",
    "table": "Employee"
  },
  "properties": {
    "employeeid": {
       // some property definitions...
    }
  },
  "validations": [],
  "relations": {
    "contracts": {
      "type": "hasMany",
      "model": "Contract",
      "foreignKey": ""
    }
  },
  "acls": [],
  "methods": []
}

当我对具有特定 idContract 执行 GET 请求时,我得到了结果:

呼叫:/api/Contracts/55

{
  "contractid": 55,
  "employeeId": 83
}

到目前为止还不错。但是当我在 Contract 上执行 GET 请求时,也得到 Employees 我希望得到这样的输出:

呼叫:/api/Contracts/55/employee

{
  "contractid": 55,
  "employee": {
      "employeeid": 83
  }
}

但是我只得到 Employee 对象而没有它的 Contract:

{
    "employeeid": 83
}

这是为什么?

我是不是做错了什么?还是我有错误的期望?

你可以/api/Contracts/55?filter[include]=employee

或:

/api/Contracts/55?filter={"include":"employee"}

您可以通过添加 "scope" 部分将合同模型默认范围设置为包括员工对象:

"scope": {
    "include": "employee"
  }