hasOneThrough style relationships
Hi folks,
I was looking through the docs for a solid way to do this but I've come up short. I wanted to use a hasOne relationship that acted like a hasManyThrough style relationship. I wanted the ability to have an intermediary model and add a relationship between two existing instances of a model via the API in the same way hasManyTrough does (I.e)
/api/{Model1}/{id}/{Model2}/rel/{fk}
Thanks in advance!
I think the only way to get this would be to code your business rules to prevent multiple Model2s for Model1.
if your through model is called "ThroughModel", then you could do:
ThroughModel.observe('before save', (ctx, next)=>{
if(!ctx.isNewInstance) return next();
// check that a throughModel with Model1.id doesn't already exist.
});
Yeah that's more or less the solution I'm using at the moment. I wanted to check that it was the only way to do it. It'd be nice if it were possible to have that /rel/ behavior for all of the relationship types.
hi @benmcmahon100 the solution post in https://github.com/strongloop/loopback/issues/3451#issuecomment-309147349 would be the workaround 👍
Unfortunately we haven't planned to support through for all relationship types yet in loopback 3, but loopback-next([email protected]) is coming and we may redesign the relations, will consider how to make through model more flexible to fit different relations.
I am closing this issue, feel free to reopen it if you want to discuss more, thanks for understanding.