0

I'm trying to create a relation between models using Loopback, but for some reason it seems like it is not saving my relations at all. But it could also be that I'm doing something wrong.

The relation looks like this:

enter image description here

So a User can be in 0 or more Pools. When they are in a Pool they are a PoolMember. And a PoolMember can add Predictions.

Right now I'm trying to create a relation between a User and Pools.

I did a slc loopback:relation and then selected User as from model. Then selected hasMany and picked Pool from the list. Then it asked if the relation has a throughModel. I selected Yes and then picked PoolMember from the list.

I'd then expect that either PoolMember.json or Pool.json has a relation defined in it. But they seem to remain unchanged.

I even deleted my project and redid everything. But have the same problem.

So am I doing something wrong here? Or is this a bug in Loopback?

Vivendi
  • 20,047
  • 25
  • 121
  • 196
  • Are you using embedded User model? Try to extend User model and then try to create relation between models. – A.Z. Aug 11 '15 at 11:45
  • @A.Z. That seemed to do the trick! Makes sense to extend the `User` object instead of using it directly. In case I want to add other properties in the future. But Loopback could've made it clear that you cannot use the `User` model as "through model". Anyway, could you please post that as an answer so I have something to accept. – Vivendi Aug 11 '15 at 13:41

1 Answers1

1

If you are using built-in models ("User" in your case), you should extend built-in model and then create relation or any other modification.

Currently you cannot modify a built-in model's required properties. If you need to do this, then create your own custom model as a replacement instead.

By default, a LoopBack application has a built-in User model defined by user.json (this file is part of the LoopBack framework. Don't modify it; rather, follow the procedure in Extending built-in models).

See Customizing models and Managing users

A.Z.
  • 1,638
  • 2
  • 18
  • 27