Overview
A belongsTo
relation sets up a one-to-one connection with another model, such that each instance of the declaring model "belongs to" one instance of the other model. For example, if your application includes customers and orders, and each order can be placed by exactly one customer.
The declaring model (Order) has a foreign key property that references the primary key property of the target model (Customer). If a primary key is not present, LoopBack will automatically add one.
Defining a belongsTo relation
Use slc loopback:relation
to create a relation between two models. You'll be prommpted to enter the name of the model, the name of related model, and other required information. The tool will then modify the Model definition JSON file (for example, /common/models/customer.json
) accordingly.
For more information, see Relation generator.
For example, here is the model JSON file for the order model in loopback-example-relations-basic:
{ "name": "Order", "base": "PersistedModel", "properties": { "description": { "type": "string" }, "total": { "type": "number" } }, "validations": [], "relations": { "customer": { "type": "belongsTo", "model": "Customer", "foreignKey": "" } }, ...
Alternatively, you can define a “belongsTo” relation in code, though in general this is not recommended:
User.belongsTo(Project, {foreignKey: ‘ownerId’});
If the declaring model doesn’t have a foreign key property, LoopBack will add a property with the same name. The type of the property will be the same as the type of the target model’s id property.
If you don’t specify them, then LoopBack derives the relation name and foreign key as follows:
- Relation name: Camel case of the model name, for example, for the "Customer" model the relation is "customer".
- Foreign key: The relation name appended with ‘Id’, for example, for relation name "customer" the default foreign key is "customerId".
Methods added to the model
Once you define the belongsTo relation, LoopBack automatically adds a method with the relation name to the declaring model class’s prototype, for example: Order.prototype.customer(...)
.
Depending on the arguments, the method can be used to get or set the owning model instance. The results of method calls are cached internally and available via later synchronous calls to the method.
Example method | Description |
---|---|
order.customer(function(err, customer) { | Get the customer for the order asynchronously |
var customer = order.customer(); | Synchronously get the results of a previous get call to customer(...) |
order.customer(customer); | Set the customer for the order |