Two distinct ReferencesMany relations in the same model, apparently considered as an unique relation.
I have a TacticsData entity which models the tactics of a soccer match, including the lineup. I have also a Tactic entity which models the specific tactic module for lineup (4-4-2, 4-4-3...) with the x and y position for each player.
TacticsData has two Tactics, one for current team and one for the opponent. They are linked to TactcsData through a ReferencesMany relation (tactics and oppositeTactics).
In other words:
TacticsData.json
{
"name": "TacticsData",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
...
},
"validations": [],
"relations": {
"match": {
"type": "belongsTo",
"model": "Match",
"foreignKey": ""
},
"tactics": {
"type": "referencesMany",
"model": "Tactics",
"foreignKey": ""
},
"oppositeTactics": {
"type": "referencesMany",
"model": "Tactics",
"foreignKey": ""
}
},
"acls": [],
"methods": {}
}
Tactics.json (here the name of the tactic is the Id, cause each lineup module is unique)
{
"name": "Tactics",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"id": true,
"required": true
},
...
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
From Loopback explorer, when I try to link the same Tactic for both the relation through endpoint (PUT /TacticsData/{id}/tactics/rel/{fk} first and PUT /TacticsData/{id}/oppositeTactics/rel/{fk} secondly), the first time it goes well, the second time it gives me a ValidationError:
{
"error": {
"statusCode": 422,
"name": "ValidationError",
"message": "The `TacticsData` instance is not valid. Details: `tactics` contains duplicate `Tactics` instance (value: undefined); `oppositeTactics` contains duplicate `Tactics` instance (value: undefined).",
"details": {
"context": "TacticsData",
"codes": {
"tactics": [
"uniqueness"
],
"oppositeTactics": [
"uniqueness"
]
},
"messages": {
"tactics": [
"contains duplicate `Tactics` instance"
],
"oppositeTactics": [
"contains duplicate `Tactics` instance"
]
}
},
}
Instead, if I put two different Tactics in the two relations, no problem. Also, when I perform a GET /TacticsData/{id}/tactics/ or a GET /TacticsData/{id}/oppositeTactics/, it returns me both the tactics, when I'm expecting just one, the tactic for my team for the first GET and the one for the opposite team for the second GET.
It's seems, since the two ReferencesMany are related to the same Model, Loopback it's considering them as one unique relation.
Is that a bug or a wanted feature? Could anyone please explain what I did wrong?
I have the sample problem, how did u solve this?
@go2smartphone Can you post your code? I think I just solved the issue for myself. Spoke too early...
i c, my code are almost same structure like you and the same result. I managed to solve this from schema design by creating additional models