Page Contents


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. This relation indicates that each instance of the declaring or source model has zero or more instances of the target model. For example, in an application with customers and orders, a customer can have many orders as illustrated in the diagram below.

hasMany relation illustration

The diagram shows target model Order has property customerId as the foreign key to reference the declaring model Customer’s primary key id.

To add a hasMany relation to your LoopBack application and expose its related routes, you need to perform the following steps:

  1. Add a property to your model to access related model instances.
  2. Modify the source model repository class to provide access to a constrained target model repository.
  3. Call the constrained target model repository CRUD APIs in your controller methods.

Defining a hasMany Relation

This section describes how to define a hasMany relation at the model level using the @hasMany decorator. The relation constrains the target repository by the foreign key property on its associated model. The following example shows how to define a hasMany relation on a source model Customer.


import {Order} from './order.model';
import {Entity, property, hasMany} from '@loopback/repository';

export class Customer extends Entity {
    type: 'number',
    id: true,
  id: number;

    type: 'string',
    required: true,
  name: string;

  @hasMany(() => Order)
  orders?: Order[];

  constructor(data: Partial<Customer>) {

The definition of the hasMany relation is inferred by using the @hasMany decorator. The decorator takes in a function resolving the target model class constructor and optionally a custom foreign key to store the relation metadata. The decorator logic also designates the relation type and tries to infer the foreign key on the target model (keyTo in the relation metadata) to a default value (source model name appended with id in camel case, same as LoopBack 3). It also calls property.array() to ensure that the type of the property is inferred properly as an array of the target model instances.

The decorated property name is used as the relation name and stored as part of the source model definition’s relation metadata. The property type metadata is also preserved as an array of type Order as part of the decoration.

A usage of the decorator with a custom foreign key name for the above example is as follows:

// import statements
class Customer extends Entity {
  // constructor, properties, etc.
  @hasMany(() => Order, {keyTo: 'custId'})
  orders?: Order[];

Configuring a hasMany relation

The configuration and resolution of a hasMany relation takes place at the repository level. Once hasMany relation is defined on the source model, then there are a couple of steps involved to configure it and use it. On the source repository, the following are required:

  • In the constructor of your source repository class, use Dependency Injection to receive a getter function for obtaining an instance of the target repository. Note: We need a getter function, accepting a string repository name instead of a repository constructor, or a repository instance, in order to break a cyclic dependency between a repository with a hasMany relation and a repository with the matching belongsTo relation.

  • Declare a property with the factory function type HasManyRepositoryFactory<targetModel, typeof> on the source repository class.
  • call the createHasManyRepositoryFactoryFor function in the constructor of the source repository class with the relation name (decorated relation property on the source model) and target repository instance and assign it the property mentioned above.

The following code snippet shows how it would look like:


import {Order, Customer} from '../models';
import {OrderRepository} from './order.repository';
import {
} from '@loopback/repository';
import {inject, Getter} from '@loopback/core';

export class CustomerRepository extends DefaultCrudRepository<
> {
  public readonly orders: HasManyRepositoryFactory<
    @inject('datasources.db') protected db: juggler.DataSource,
    getOrderRepository: Getter<OrderRepository>,
  ) {
    super(Customer, db);
    this.orders = this.createHasManyRepositoryFactoryFor(

The following CRUD APIs are now available in the constrained target repository factory orders for instances of customerRepository:

  • create for creating a target model instance belonging to customer model instance (API Docs)
  • find finding target model instance(s) belonging to customer model instance (API Docs)
  • delete for deleting target model instance(s) belonging to customer model instance (API Docs)
  • patch for patching target model instance(s) belonging to customer model instance (API Docs)

For updating (full replace of all properties on a PUT endpoint for instance) a target model you have to directly use this model repository. In this case, the caller must provide both the foreignKey value and the primary key (id). Since the caller already has access to the primary key of the target model, there is no need to go through the relation repository and the operation can be performed directly on DefaultCrudRepository for the target model (OrderRepository in our example).

Using hasMany constrained repository in a controller

The same pattern used for ordinary repositories to expose their CRUD APIs via controller methods is employed for hasMany repositories. Once the hasMany relation has been defined and configured, controller methods can call the underlying constrained repository CRUD APIs and expose them as routes once decorated with Route decorators. It will require the value of the foreign key and, depending on the request method, a value for the target model instance as demonstrated below.


import {post, param, requestBody} from '@loopback/rest';
import {CustomerRepository} from '../repositories/';
import {Customer, Order} from '../models/';
import {repository} from '@loopback/repository';

export class CustomerOrdersController {
    protected customerRepository: CustomerRepository,
  ) {}

  async createOrder(
    @param.path.number('id') customerId: typeof,
    @requestBody() orderData: Order,
  ): Promise<Order> {
    return await this.customerRepository.orders(customerId).create(orderData);

In LoopBack 3, the REST APIs for relations were exposed using static methods with the name following the pattern __{methodName}__{relationName}__ (e.g. Customer.__find__orders). We recommend to create a new controller for each relation in LoopBack 4. First, it keeps controller classes smaller. Second, it creates a logical separation of ordinary repositories and relational repositories and thus the controllers which use them. Therefore, as shown above, don’t add order-related methods to CustomerController, but instead create a new CustomerOrdersController class for them.