Loopback: Multiple hasAndBelongsToMany relations between the same two models

Created on 3 Nov 2014  路  6Comments  路  Source: strongloop/loopback

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?

Most helpful comment

@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

All 6 comments

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'});

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'});

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.

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

@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

Was this page helpful?
0 / 5 - 0 ratings