Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between hasMany and referencesMany in Strongloop loopback

I read that embedsMany (in case of non relational db) puts the embedded models in the parent model document. While hasMany creates a new collection of child model and a relation is created between parent collection and child collection. What about referencesMany?

There is also this example project, where customer referencesMany Account and hasMany Order. I am not understanding the difference.

like image 239
Sanandrea Avatar asked Feb 19 '16 10:02

Sanandrea


People also ask

What does. hasMany do?

A hasMany relation denotes a one-to-many connection of a model to another model through referential integrity. The referential integrity is enforced by a foreign key constraint on the target model which usually references a primary key on the source model.

What is LoopBack 3?

The LoopBack framework is a set of Node. js modules that you can use independently or together to quickly build REST APIs. A LoopBack application interacts with data sources through the LoopBack model API, available locally within Node. js, remotely over REST, and via native client APIs for iOS, Android, and HTML5.


1 Answers

embedsMany: puts all child data (naturally and relation) in itself model. each childs hasn't any id and can't be reusable in another rows). it needs just one collection for child and parent model.

referencesMany: puts just id of child relation (reference id) in itself model and puts data in child model (no need to store parent model id in child model and child model id is reusable for another models). it needs an actual two collection for parent and child model.

hasMany: puts child data and parent id in child model. it needs too an actual two collection;

like image 58
Vahid Moradi Avatar answered Oct 19 '22 09:10

Vahid Moradi