Create a relation between two LoopBack models

InstructorBram Borggreve

Share this video with your friends

Send Tweet

A relation in LoopBack is a way to associate the data from multiple models with each other. LoopBack supports various types of relations each with a different use case.

In this lesson we will create a second model called Category and create a relation between the Category and the Product models. To do this we will first add a property to the Product model called categoryId. This will allow us to store for each Product the category it belongs to. We then move on to create the actual relationships.

Our first relationship defines that a Category hasMany Products and the second one that a Product belongsTo a Category.

After defining these relations we will use the API Explorer to see how we can interact with this related data.

Manuel Escamilla
~ 7 years ago

In the first relation I was prompted with two more questions, I selected the defaults.

? Allow the relation to be nested in REST APIs: No ? Disable the relation from being included: No