Loopback relation HasAndBelongsToMany with a same model (add friend / make friend Facebook like)

209 views
Skip to first unread message

Brian Duong

unread,
Oct 7, 2016, 4:29:29 AM10/7/16
to LoopbackJS
This is a very common case:
We have an User model, now we want an user make friend with each other.
I tried to create a HasAndBelongsToMany like that:


I think its is very reasonable but it doesn't work...


of course this table UsersUsers was generated by Loopback!
It would be perfect is the foreign key is friendId (instead of undefined) and its value is an User ObjectId (instead of String)

Please advise, thanks in advance!

Alex Megalokonomos

unread,
Oct 7, 2016, 4:40:01 AM10/7/16
to loopb...@googlegroups.com
HasManyThrough ?

with an intermediate table friendships like (friendshipid)  ,userid, friendid ? which can also be expanded with relationship type etc.?

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/loopbackjs/2702a885-0fce-4e65-a5fb-3a981994b779%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex Megalokonomos

unread,
Oct 7, 2016, 4:49:19 AM10/7/16
to loopb...@googlegroups.com
In essense it's exactly the same as what loopback tries to create automatically but allows you to declare the table yourself so a) you can have friendid instead of undefined (because LB tries to set the column names programmatically and they end up both as UsersID so once is undefined) and b) you can extend the intermediate table with extra metadata  such as relationshiptype, date of friendship etc.

Brian Duong

unread,
Oct 7, 2016, 5:01:50 AM10/7/16
to LoopbackJS
Already tried but it also does not work:
  • Created the table


  • Added the relationship

  • And the result:



Thanks anyway for your reply!

On Friday, October 7, 2016 at 3:40:01 PM UTC+7, Alex Megalokonomos wrote:
HasManyThrough ?

with an intermediate table friendships like (friendshipid)  ,userid, friendid ? which can also be expanded with relationship type etc.?
On Fri, Oct 7, 2016 at 11:29 AM, Brian Duong <b...@eaglit.com> wrote:
This is a very common case:
We have an User model, now we want an user make friend with each other.
I tried to create a HasAndBelongsToMany like that:


I think its is very reasonable but it doesn't work...


of course this table UsersUsers was generated by Loopback!
It would be perfect is the foreign key is friendId (instead of undefined) and its value is an User ObjectId (instead of String)

Please advise, thanks in advance!

--
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.

Brian Duong

unread,
Oct 7, 2016, 5:06:17 AM10/7/16
to loopb...@googlegroups.com
Can you tell me more details on how can i declare the table UsersUsers manually and set one for the id, one for the fk ?

You also didn't notice the second problem of the case, fk key was saved as String instead of User Object Id


On Friday, October 7, 2016 at 3:49:19 PM UTC+7, Alex Megalokonomos wrote:
In essense it's exactly the same as what loopback tries to create automatically but allows you to declare the table yourself so a) you can have friendid instead of undefined (because LB tries to set the column names programmatically and they end up both as UsersID so once is undefined) and b) you can extend the intermediate table with extra metadata  such as relationshiptype, date of friendship etc.
On Fri, Oct 7, 2016 at 11:39 AM, Alex Megalokonomos <al...@clockwork.gr> wrote:
HasManyThrough ?

with an intermediate table friendships like (friendshipid)  ,userid, friendid ? which can also be expanded with relationship type etc.?
On Fri, Oct 7, 2016 at 11:29 AM, Brian Duong <b...@eaglit.com> wrote:
This is a very common case:
We have an User model, now we want an user make friend with each other.
I tried to create a HasAndBelongsToMany like that:


I think its is very reasonable but it doesn't work...


of course this table UsersUsers was generated by Loopback!
It would be perfect is the foreign key is friendId (instead of undefined) and its value is an User ObjectId (instead of String)

Please advise, thanks in advance!

--
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.

Alex Megalokonomos

unread,
Oct 7, 2016, 5:40:40 AM10/7/16
to loopb...@googlegroups.com
if you're using a through model that you have defined you need to declare the right properties for it in the model declaration

To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/loopbackjs/0c91ddf0-e2bc-49f6-9fd1-bd6b1e0b6d13%40googlegroups.com.

Brian Duong

unread,
Oct 7, 2016, 5:58:18 AM10/7/16
to LoopbackJS
how can Loopback know which property is the primary key and one other is foreign key ?

Alex Megalokonomos

unread,
Oct 7, 2016, 6:39:06 AM10/7/16
to loopb...@googlegroups.com
Check: https://docs.strongloop.com/display/public/LB/HasManyThrough+relations

You should be able to define the relation using the  models json files (rather than the relations generator)

 ...Haven't tested it though


To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/loopbackjs/77e8d05c-be22-481c-b3f2-3aa5ee77de7a%40googlegroups.com.

Brian Duong

unread,
Oct 7, 2016, 7:47:05 AM10/7/16
to LoopbackJS
Solved after reading this one https://github.com/strongloop/loopback/issues/1993.

Closed
Reply all
Reply to author
Forward
0 new messages