Julian
Julian

Reputation: 495

How to define that a model has many of the same model in Strongloop Loopback?

What should a models.json look like when I want to define that a Organization has many Organizations?

I tried to define a hasMany through relationshio, using an intermediate model called clients but it didn't work:

"organization": {
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "relations": {
    "clients": {
      "type": "hasMany",
      "model": "organization",
      "foreignKey": "clientId",
      "through": "client"
    }
  }
  "client": {
    "properties": {
      "organizationId": {
        "type": "number",
        "id": true
      },
      "clientId": {
        "type": "number",
        "id": true
      }
    },
    "relations": {
      "organization": {
        "type": "belongsTo",
        "model": "organization",
        "foreignKey": "organizationId"
      },
      "client": {
        "type": "belongsTo",
        "model": "organization",
        "foreignKey": "clientId"
      }
    }
  }

Upvotes: 1

Views: 2163

Answers (1)

Raymond Feng
Raymond Feng

Reputation: 1536

The question has been answered by: https://groups.google.com/d/msg/loopbackjs/H7ivcbLAaHo/C9iQop4RXAYJ

Upvotes: 2

Related Questions