Skip to main content
Version: 4.5

Entity References

Every single entity relation is mapped to an entity reference. Reference is an entity that has only its identifier. This reference is stored in identity map so you will get the same object reference when fetching the same document from database.

You can call await wrap(entity).init() to initialize the entity. This will trigger database call and populate itself, keeping the same reference in identity map.

const author = orm.em.getReference('');console.log(; // accessing the id will not trigger any db callconsole.log(author.isInitialized()); // falseconsole.log(; // undefined
await wrap(author).init(); // this will trigger db callconsole.log(author.isInitialized()); // trueconsole.log(; // defined

Better Type-safety with Reference<T> Wrapper#

When you define @ManyToOne and @OneToOne properties on your entity, TypeScript compiler will think that desired entities are always loaded:

@Entity()export class Book {
  @PrimaryKey()  id!: number;
  @ManyToOne()  author!: Author;
  constructor(author: Author) { = author;  }
const book = await orm.em.findOne(Book, 1);console.log( instanceof Author); // trueconsole.log(; // falseconsole.log(; // undefined as `Author` is not loaded yet

You can overcome this issue by using the Reference<T> wrapper. It simply wraps the entity, defining load(): Promise<T> method that will first lazy load the association if not already available. You can also use unwrap(): T method to access the underlying entity without loading it.

You can also use load<K extends keyof T>(prop: K): Promise<T[K]>, which works like load() but returns the specified property.

import { Entity, IdentifiedReference, ManyToOne, PrimaryKey, Reference } from '@mikro-orm/core';
@Entity()export class Book {
  @PrimaryKey()  id!: number;
  @ManyToOne(() => Author, { wrappedReference: true })  author: IdentifiedReference<Author>;
  constructor(author: Author) { = Reference.create(author);  }
const book = await orm.em.findOne(Book, 1);console.log( instanceof Reference); // trueconsole.log(; // falseconsole.log(; // type error, there is no `name` propertyconsole.log(; // undefined as author is not loadedconsole.log((await'name'))); // ok, loading the author firstconsole.log((await; // ok, author already loadedconsole.log(; // ok, author already loaded

There are also getEntity() and getProperty() methods that are synchronous getters, that will first check if the wrapped entity is initialized, and if not, it will throw and error.

const book = await orm.em.findOne(Book, 1);console.log( instanceof Reference); // trueconsole.log(; // falseconsole.log(; // Error: Reference<Author> 123 not initializedconsole.log('name')); // Error: Reference<Author> 123 not initializedconsole.log((await'name'))); // ok, loading the author firstconsole.log('name')); // ok, author already loaded

If you use different metadata provider than TsMorphMetadataProvider (e.g. ReflectMetadataProvider), you will also need to explicitly set wrappedReference parameter:

@ManyToOne(() => Author, { wrappedReference: true })author!: IdentifiedReference<Author>;

Assigning to Reference Properties#

When you define the property as Reference wrapper, you will need to assign the Reference to it instead of the entity. You can create it via Reference.create() factory, or use wrapped parameter of em.getReference():

const book = await orm.em.findOne(Book, 1);const repo = orm.em.getRepository(Author); = repo.getReference(2, true);
// same = Reference.create(repo.getReference(2));await orm.em.flush();

Another way is to use toReference() method available as part of WrappedEntity interface:

const author = new Author(...) = wrap(author).toReference();

If the reference already exist, you can also re-assign to it via set() method: Author(...));

What is IdentifiedReference?#

IdentifiedReference is an intersection type that adds primary key property to the Reference interface. It allows to get the primary key from Reference instance directly.

By default, we try to detect the PK by checking if a property with a known name exists. We check for those in order: _id, uuid, id - with a way to manually set the property name via PrimaryKeyProp symbol ([PrimaryKeyProp]?: 'foo';).

We can also override this via second generic type argument.

const book = await orm.em.findOne(Book, 1);console.log(; // ok, returns the PK

You can also have non-standard primary key:

author: IdentifiedReference<Author, 'myPrimaryKey'>;
const book = await orm.em.findOne(Book, 1);console.log(; // ok, returns the PK

For MongoDB, define the PK generic type argument as 'id' | '_id' to access both string and ObjectId PK values:

@Entity()export class Book {
  @PrimaryKey()  _id!: ObjectId;
  @SerializedPrimaryKey()  id!: string;
  @ManyToOne(() => Author, { wrappedReference: true })  author!: IdentifiedReference<Author, 'id' | '_id'>;
const book = await orm.em.findOne(Book, 1);console.log(; // ok, returns string PKconsole.log(; // ok, returns ObjectId PK

As opposed to EntityHelper.init() which always refreshes the entity, Reference.load() method will query the database only if the entity is not already loaded in Identity Map.