Reputation: 563
I have two models users
and appointments
.
The users
model is like below-
{
"users": {
"0": {
"id": "1",
"name": "test1",
"role": "doctor"
},
"1": {
"id": "2",
"name": "test2",
"role": "patient"
},
"2": {
"id": "3",
"name": "test3",
"role": "support"
}
}
}
Now in the above model , if the role is doctor , we are calling it as doctor_id
, if patient
then patient_id
and so on.
Now my appointment model is below->
{
"name": "appointments",
"plural": "appointments",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"appointmentDate": {
"type": "date"
},
"appointmentTime": {
"type": "string"
},
"doctorId": {
"type": "string"
},
"patientId": {
"type": "string"
}
},
"validations": [],
"relations": {
"Doctor": {
"type": "belongsTo",
"model": "users",
"foreignKey": "doctorId"
},
"Patient": {
"type": "belongsTo",
"model": "users",
"foreignKey": "patientId"
}
},
"acls": [],
"methods": {}
}
So when i try to GET
all appointments , it is not sending the relation data from users
. If i add single relation it work as expected , but not working with multiple relations from same model.
Thanks in advance,
Upvotes: 1
Views: 1171
Reputation: 3523
If you go to the belongsTo documentation you can see that the foreignKey
field is empty, so you need to remove it from your relations definition.
You probably also want to define the other side of the relation.
I.e. define a hasMany relation users hasMany appointments
:
{
"name": "users",
"base": "PersistedModel",
...
"relations": {
"doctors": {
"type": "hasMany",
"model": "appointments",
"foreignKey": "doctorId"
},
"patients": {
"type": "hasMany",
"model": "appointments",
"foreignKey": "patientId"
},
...
}
However, what you are trying to do might not be supported (and in fact I'm not even sure if it makes sense for LoopBack).
You can check the documentation on polymorphic relations and even though it's a work in progress there's no mention of having Model A hasMany Model B
through more than one foreignKey
at the same time.
LoopBack would need to have some logic to search under the first foreign key and if nothing is found then searching under the other foreign key, and I'm not sure if this complex composition is supported.
Why not define two models, one for Doctors and one for Patients and have two different relations instead? You can even go to the documentation on HasManyThrough relations and you can see an example that models something very similar to what you are trying to do.
Upvotes: 0
Reputation: 849
Including my previous comment to give context:
The way I did it, I believe foreignkey should be "" for both. You should not define doctorId and patientId and you probably have to define two hasMany relations in you user class with foreign key "Doctor" and "Patient"
To give an example here are the relations defined in my user class (called customer)
"commentsWritten": {
"type": "hasMany",
"model": "comment",
"foreignKey": "sourceCustomerId"
},
"commentsReceived": {
"type": "hasMany",
"model": "comment",
"foreignKey": "targetCustomerId"
},
then in my comment definition I have the following
"properties": {
...
"targetCustomerId": {
"type": {
"required": true
}
},
"sourceCustomerId": {
"type": {
"required": true
}
}
},
"relations": {
"targetCustomer": {
"type": "belongsTo",
"model": "customer",
"foreignKey": ""
},
"sourceCustomer": {
"type": "belongsTo",
"model": "customer",
"foreignKey": ""
}
},
Please note, that I did define the properties (ids) but, if I remember correctly, it was only so I could force them to be not null, i.e. you should not need it.
Upvotes: 0