loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Multiple hasAndBelongsToMany relations between the same two models

Open wtfribley opened this issue 12 years ago • 6 comments

common/models/me.json

{
  "name": "Me",
  "base": "PersistedModel",
  "properties": {}
  "relations": {
    "aunts": {
      "type": "hasAndBelongsToMany",
      "model": "Relative",
      "foreignKey": ""
    },
    "uncles": {
      "type": "hasAndBelongsToMany",
      "model": "Relative",
      "foreignKey": ""
    }
  }
}

common/models/relative.js

{
  "name": "Relative",
  "base": "PersistedModel",
  "properties": {},
  "relations": {}
}

This kind of relationship -- i.e. multiple relationships between the same two models -- works for hasMany relationships. But that same behavior appears to be unsupported for hasAndBelongsToMany. Are there plans to add support? Is there a way to "manually" set up this kind of relationship?

wtfribley avatar Nov 03 '14 20:11 wtfribley

It seems that you are looking for http://docs.strongloop.com/display/LB/Polymorphic+relations.

raymondfeng avatar Nov 03 '14 21:11 raymondfeng

Ah, thanks!

Although, while polymorphic relations may work for this use case (from some initial testing it looks like they actually won't work) - I'm not actually interested in setting up a true polymorphic relationship... i.e. Relative is always related to Me, the relationship doesn't seem to be polymorphic.

I'm simply looking for the same behavior as hasAndBelongsToMany (complete with REST endpoints like /Me/:id/aunts/rel/:fk), with support for multiple foreign keys (as is already implemented for hasMany). Polymorphic relations might solve the problem, but do they solve it in the best way? In my case there's no need to store the name of the related model, only the foreign key.

Just to clarify, here's code instead of JSON:

Me.hasAndBelongsToMany(Relative, {as: 'aunts', foreignKey: 'auntsId'});
Me.hasAndBelongsToMany(Relative, {as: 'uncles', foreignKey: 'unclesId'});

wtfribley avatar Nov 03 '14 23:11 wtfribley

Please note hasAndBelongsToMany will use a default through model MeRelative if not configured. To support two types of connections, try to customize the through model via through property, for example:

Me.hasAndBelongsToMany(Relative, {as: 'aunts', foreignKey: 'auntsId', through: 'MeAunt'});
Me.hasAndBelongsToMany(Relative, {as: 'uncles', foreignKey: 'unclesId', through: 'MeUncle'});

raymondfeng avatar Nov 04 '14 00:11 raymondfeng

Oh excellent!

I know the docs are currently getting an overhaul - perhaps highlighting this use of through would be helpful! ...it's probably something that seems perfectly obvious in the RDBMS world, perhaps less so for document database folks.

wtfribley avatar Nov 04 '14 00:11 wtfribley

@raymondfeng can you shed light on if this is possible to do with just the json model definitions?

genre.json

"posts": {
  "type": "hasAndBelongsToMany",
  "model": "Post"
},

post.json

"genres": {
  "type": "hasAndBelongsToMany",
  "model": "Genre"
},

this results in the table in postgres genrepost. what I really want is the table postgenre. when I add "through": "postGenre" to both definitions and migrate the models dont get created.

dancingshell avatar Sep 19 '16 22:09 dancingshell

@raymondfeng @wtfribley sorry to bring this issue back but is this feature currently available? Seems that hasAndBelongsToMany relations are omitting the through model that's specified and I can't find it in the docs

hyperd avatar Apr 18 '17 19:04 hyperd