Skip to content

Relation cardinality

@atLeastOne is a JSDoc tag, not a TypeScript decorator.

A collection relation (1:N or M:N) renders as zero-or-more by default. Tag the collection property with @atLeastOne to render that collection side as one-or-more instead:

@Entity()
export class Author {
/** @atLeastOne */
@OneToMany(() => Post, (post) => post.author)
posts = new Collection<Post>(this);
}

This turns the ERD edge Post }o--|| Author into Post }|--|| Author. It is a documentation hint only — MikroORM has no schema-level minimum, and the count is not enforced. (Mermaid distinguishes only zero-or-more vs. one-or-more, so no larger minimum can be expressed.)

A relation edge has two ends, set independently:

  • Single (1) side (@ManyToOne, or the owning @OneToOne) — read from your schema automatically, no tag needed: exactly-one (||) by default, zero-or-one (o|) when nullable: true. The inverse side of an owning one-to-one is always zero-or-one — a unique FK caps references at one but does not require one.
  • Collection (N) side (@OneToMany / @ManyToMany) — zero-or-more by default; @atLeastOne raises that side to one-or-more (}o → }|, or o{ → |{, depending on which side of the edge the collection is rendered on).

The four combinations (Post ↔ Author):

Post }o--|| Author → author 0+ posts, post exactly 1 author (default)
Post }o--o| Author → author 0+ posts, post 0-or-1 author (nullable: true)
Post }|--|| Author → author 1+ posts, post exactly 1 author (@atLeastOne)
Post }|--o| Author → author 1+ posts, post 0-or-1 author (both)