Demiro-FE-Architect
Demiro-FE-Architect

Reputation: 3740

Loopback: Relation Through - not working

So, I am stuck on an issue, that should be simple, and I am sure I am missing something obvious

I am following this documentation:

so I have 3 tables

client, team, client-team

client.json

{
  "name": "client",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "teams": {
      "type": "hasMany",
      "model": "team",
      "foreignKey": "teamId",
      "through": "client-team"
    }
  },
  "acls": [],
  "methods": {}
}

team.json

{
  "name": "team",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "type": {
      "type": "string",
      "required": true,
      "default": "first-team"
    },
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "clients": {
      "type": "hasMany",
      "model": "client",
      "foreignKey": "clientId",
      "through": "client-team"
    }
  },
  "acls": [],
  "methods": {}
}

client-team.json

{
  "name": "client-team",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "clientId": {
      "type": "string",
      "required": true
    },
    "teamId": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "client": {
      "type": "belongsTo",
      "model": "Client",
      "foreignKey": "clientId"
    },
    "team": {
      "type": "belongsTo",
      "model": "Team",
      "foreignKey": "teamId"
    }
  },
  "acls": [],
  "methods": {}
}

so all the relations set correctly (I think)...

then in my clients I do have 1 client

[
  {
    "name": "Client name",
    "id": "59876185508eb519385779c6"
  }
]

and in my teams I have many, but for sure this:

[
  {
    "type": "type",
    "name": "Team name",
    "id": "5ae8a37add2989a32d37f83d"
  }
]

And then I go to my

localhost:3000/explorer

To POST a client-team

like this

{
    "clientId": "59876185508eb519385779c6",
    "teamId": "5ae8a37add2989a32d37f83d"
}

and I get the 200 response with:

{
  "clientId": "59876185508eb519385779c6",
  "teamId": "5ae8a37add2989a32d37f83d",
  "id": "5ae961873a7e3b33f0579fc3"
}

so the connection is there....

But then, when I go to "GET client/id" and do

id: 59876185508eb519385779c6 filter: {"include":["teams"]}

this is the response

{
  "name": "Chelsea FC",
  "id": "59876185508eb519385779c6",
  "teams": []
}

The same happens in the "GET teams/id" and I use

id: 5ae8a37add2989a32d37f83d filter: {"include":["clients"]}

or if I go to "GET teams/{id}/clients" and put id: 5ae8a37add2989a32d37f83d

I get

[]

So what am I doing wrong? I am sure I am missing a stupid, obvious thing :/

using mongo if that makes any difference

Upvotes: 0

Views: 157

Answers (1)

Maxim Sharai
Maxim Sharai

Reputation: 614

There are three issues here:

  1. You described mongodb identifiers as string, that's why you store strings in database instead of object ids. (it's not required as the datasource should understand the real type)
  2. Your models start from lower case letter. The same should be in the relations also. (the first part of the problem, it's fixing issue with ids)
  3. Incorrect relations for client and team models (the second part of the problem, it's fixing includes)

client-team.json

{
  "name": "client-team",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "clientId": {
      "type": "objectId", // !!! changed (not required)
      "required": true
    },
    "teamId": {
      "type": "objectId", // !!! changed (not required)
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "client": {
      "type": "belongsTo",
      "model": "client",  // !!! changed
      "foreignKey": "clientId"
    },
    "team": {
      "type": "belongsTo",
      "model": "team",  // !!! changed
      "foreignKey": "teamId"
    }
  },
  "acls": [],
  "methods": {}
}

client.json

{
  "name": "client",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "teams": {
      "type": "hasMany",
      "model": "team",
      "foreignKey": "clientId", // !!! changed (we describing id of this model, not team)
      "through": "client-team"
    }
  },
  "acls": [],
  "methods": {}
}

team.json

{
  "name": "team",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "type": {
      "type": "string",
      "required": true,
      "default": "first-team"
    },
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "clients": {
      "type": "hasMany",
      "model": "client",
      "foreignKey": "teamId", // !!! changed (the same as the previous)
      "through": "client-team"
    }
  },
  "acls": [],
  "methods": {}
}

Upvotes: 1

Related Questions