Version: Next

Relationship Loading Strategies

SQL only feature

Controls how relationships get loaded when querying. By default, populated relationships are loaded via the select-in strategy. This strategy issues one additional SELECT statement per relation being loaded.

The loading strategy can be specified both at mapping time and when loading entities.

For example, given the following entities:

import { Entity, LoadStrategy, OneToMany, ManyToOne } from '@mikro-orm/core';
export class Author {
@OneToMany(() => Book, b =>
books = new Collection<Book>(this);
export class Book {
author: Author;

The following will issue two SQL statements. One to load the author and another to load all the books belonging to that author:

const author = await orm.em.findOne(Author, 1, ['books']);

If we update the Author.books mapping to the following:

import { Entity, LoadStrategy, OneToMany } from '@mikro-orm/core';
export class Author {
entity: () => Book,
mappedBy: b =>,
strategy: LoadStrategy.JOINED,
books = new Collection<Book>(this);

The following will issue one SQL statement:

const author = await orm.em.findOne(Author, 1, ['books']);

You can also specify the load strategy as needed. This will override whatever strategy is declared in the mapping. This also works for nested populates:

// one level
const author = await orm.em.findOne(Author, 1, { populate: { books: LoadStrategy.JOINED } });
// two levels
const author = await orm.em.findOne(Author, 1, { populate: {
books: [LoadStrategy.JOINED, { publisher: LoadStrategy.JOINED }]
} });

Changing the loading strategy globally

You can use loadStrategy option in the ORM config:

loadStrategy: LoadStrategy.JOINED,

This value will be used as the default, specifying the loading strategy on property level has precedence, as well as specifying it in the FindOptions.

Last updated on by Martin Adámek