0

I have a couple relations I want to model,

  1. A Message belongs to 2 Profile's (a sender and a recipient)
  2. An Enrollment consists of a Profile and a Curriculum.

I tried to do #1 using 2 hasOne but ended up with Profile.messageId.

{
  "name": "Message",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "number",
      "id": true,
      "required": true
    },
    "text": {
      "type": "string",
      "required": true
    },
    "created": {
      "type": "date",
      "required": true
    },
    "seen": {
      "type": "boolean",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "sender": {
      "type": "hasOne",
      "model": "Profile",
      "foreignKey": ""
    },
    "recipient": {
      "type": "hasOne",
      "model": "Profile",
      "foreignKey": ""
    }
  },
  "acls": [],
  "methods": []
}

Same problem w/ #2...

{
  "name": "Enrollment",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "number",
      "id": true,
      "required": true
    },
    "created": {
      "type": "date",
      "required": true
    },
    "currentPage": {
      "type": "string",
      "comments": "What page are they on in this curriculum?"
    }
  },
  "validations": [],
  "relations": {
    "curriculums": {
      "type": "hasOne",
      "model": "Curriculum",
      "foreignKey": ""
    },
    "profiles": {
      "type": "hasOne",
      "model": "Profile",
      "foreignKey": ""
    }
  },
  "acls": [],
  "methods": []
}
Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
broinjc
  • 2,619
  • 4
  • 28
  • 44
  • Why are you using 2 hasOne relations instead of a hasMany? https://docs.strongloop.com/display/public/LB/HasMany+relations – JSimonsen Sep 03 '15 at 21:46
  • 1
    Should I do 2 hasMany relations with different foreignkey names (sender, recipient)? – broinjc Sep 04 '15 at 00:34
  • And for the Enrollment - It will be a Through model. Profile will have a hasManyThrough relation with curriculum and visa versa. https://docs.strongloop.com/display/public/LB/HasManyThrough+relations – broinjc Sep 04 '15 at 01:09
  • @broinjc You should try that. If that worked post it as an answer and accept it. I believe the problem is happening because you didn't specify the foreignkey so loopback is assigning the default. – Farid Nouri Neshat Sep 15 '15 at 13:55
  • @FaridNouriNeshat - I'll post my solution soon :) thanks for the reminder. Still working it with sample data just to make sure I've got it just how I want it. – broinjc Sep 15 '15 at 22:47

0 Answers0