I'm trying to setup a basic model relation in Loopback by creating a belongsTo
relationship.
I have two models defined like this:
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": []
}
When I do a GET
request on a Contract
with a specfic id
I get the result:
Calling: /api/Contracts/55
{
"contractid": 55,
"employeeId": 83
}
Which is fine so far. But when I do a GET
request on Contract
, also getting the Employee
s I expect to get an ouput like this:
Calling: /api/Contracts/55/employee
{
"contractid": 55,
"employee": {
"employeeid": 83
}
}
But instead I'm only getting the Employee
object without its Contract
:
{
"employeeid": 83
}
Why is that?
Am I doing something wrong? Or do I have the wrong expectations?
You can do
/api/Contracts/55?filter[include]=employee
or:
You can set the Contract model default scope to include employee objects by adding a "scope" section: