Recursive many to many relationship problem loopback

550 views
Skip to first unread message

sanjee...@navyuginfo.com

unread,
Apr 20, 2015, 3:51:42 AM4/20/15
to loopb...@googlegroups.com
Hi all,I was trying to make hasMany relation of model with the same model itself like my model was ticket and in that relations defined are parentTickets and childTickets which are array of tickets and i made a mapping table 'ticketRelation' which is mapping table for the has many relationship.My models are following-

ticket model-
"relations":{
   "parentTickets":{
      "type":"hasMany",
      "model":"ticket",
      "foreignKey":"childId",
      "through":"ticketRelation"
    },
  "childTickets":{
      "type":"hasMany",
      "model":"ticket",
      "foreignKey":"parentId",
      "through":"ticketRelation"
  }
}
ticketRelation-

"relations":{
    "pticket": {
      "type": "belongsTo",
      "model": "ticket",
      "foreignKey": "parentId"
    },
    "ticket": {
      "type": "belongsTo",
      "model": "ticket",
      "foreignKey": "childId"
    }
}

My sample data is-
ticket id =1 has child tickets with id =2,3

so when i try to find parentTickets in ticket model by the following URL
http//localhost:3000/api/tickets?filter[include]=childTickets

it give me correct result ie ticket-id =1,childTickets=2,3  

but whenever i try to find parentTickets for ticket by the following URL, it is not giving me correct result 

http//localhost:3000/api/tickets?filter[include]=parentTickets

The data retrieved is-
ticket-id=1, parentTicket -1

so the problem i noticed is might be that loopback is expecting the relation name to be same as that of model name which we are specifying in the relation in the mapping table( ticketRelation) to retrieve the data.

Roberto Nemirovsky

unread,
May 12, 2015, 5:48:47 PM5/12/15
to loopb...@googlegroups.com
I'd tried something similiar (with error). I wonder if someone could help me.

{
  "name": "Profile",
  ...
  "relations": {
    "friends": {
      "type": "hasMany",
      "model": "Profile",
      "foreignKey": "fkUserId",
      "through": "Relationship"
    },
    "friendOf": {
      "type": "hasMany",
      "model": "Profile",
      "foreignKey": "fkFriendId",
      "through": "Relationship"
    }
  }
  ...
}


{
  "name": "Relationship",
  ...
  "relations": {
    "user": {
      "type": "belongsTo",
      "model": "Profile",
      "foreignKey": "fkUserId"
    },
    "friend": {
      "type": "belongsTo",
      "model": "Profile",
      "foreignKey": "fkFriendId"
    }
  }
  ...
}

With this error:

http://127.0.0.1:3000/api/Profiles?filter[include]=friends

{
  "error": {
    "name": "Error",
    "status": 500,
    "message": "Relation \"profile\" is not defined for Relationship model",
    "stack": "Error: Relation \"profile\" is not defined for Relationship model\n    at processIncludeItem (F:\\Projetos\\git\\RSCC\\temp2\\server\\node_modules\\loopback-datasource-juggler\\lib\\include.js:144:10)\n    at F:\\Projetos\\git\\RSCC\\temp2\\server\\node_modules\\loopback-datasource-juggler\\lib\\include.js:118:5\n    at F:\\Projetos\\git\\RSCC\\temp2\\server\\node_modules\\loopback-datasource-juggler\\node_modules\\async\\lib\\async.js:125:13\n    at Array.forEach (native)\n    at _each (F:\\Projetos\\git\\RSCC\\temp2\\server\\node_modules\\loopback-datasource-juggler\\node_modules\\async\\lib\\async.js:46:24)\n    at Object.async.each (F:\\Projetos\\git\\RSCC\\temp2\\server\\node_modules\\loopback-datasource-juggler\\node_modules\\async\\lib\\async.js:124:9)\n    at Function.Inclusion.include (F:\\Projetos\\git\\RSCC\\temp2\\server\\node_modules\\loopback-datasource-juggler\\lib\\include.js:117:9)\n    at F:\\Projetos\\git\\RSCC\\temp2\\server\\node_modules\\loopback-datasource-juggler\\lib\\connectors\\memory.js:369:33\n    at F:\\Projetos\\git\\RSCC\\temp2\\server\\node_modules\\loopback\\node_modules\\continuation-local-storage\\node_modules\\async-listener\\glue.js:188:31\n    at process._tickDomainCallback (node.js:381:11)"
  }
}

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Roberto Nemirovsky

unread,
May 14, 2015, 10:37:48 PM5/14/15
to loopb...@googlegroups.com
After some debugging I've realized the problem. The solution has been documented in https://github.com/strongloop/loopback-datasource-juggler/pull/281

Unfortunatelly, the parameter "keyThrough" is missing in docs.strongloop.com.

Regards,
Roberto
Reply all
Reply to author
Forward
0 new messages