Relations FAQ

How to create self referencing relation

Self-referencing relations are relations which have a relation to themself.This is useful when you are storing entities in a tree-like structures.Also “adjacency list” pattern is implemented used self-referenced relations.For example, you want to create categories tree in your application.Categories can nest categories, nested categories can nest other categories, etc.Self-referencing relations come handy here.Basically self-referencing relations are just regular relations that targets entity itself.Example:

  1. import {Entity, PrimaryGeneratedColumn, Column, ManyToOne, OneToMany} from "typeorm";
  2. @Entity()
  3. export class Category {
  4. @PrimaryGeneratedColumn()
  5. id: number;
  6. @Column()
  7. title: string;
  8. @Column()
  9. text: string;
  10. @ManyToOne(type => Category, category => category.childCategories)
  11. parentCategory: Category;
  12. @OneToMany(type => Category, category => category.parentCategory)
  13. childCategories: Category[];
  14. }

How to use relation id without joining relation

Sometimes you want to have in your object id of the related object without loading it.For example:

  1. import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
  2. @Entity()
  3. export class Profile {
  4. @PrimaryGeneratedColumn()
  5. id: number;
  6. @Column()
  7. gender: string;
  8. @Column()
  9. photo: string;
  10. }
  1. import {Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn} from "typeorm";
  2. import {Profile} from "./Profile";
  3. @Entity()
  4. export class User {
  5. @PrimaryGeneratedColumn()
  6. id: number;
  7. @Column()
  8. name: string;
  9. @OneToOne(type => Profile)
  10. @JoinColumn()
  11. profile: Profile;
  12. }

When you load a user without profile joined you won’t have any information about profile in your user object,even profile id:

  1. User {
  2. id: 1,
  3. name: "Umed"
  4. }

But sometimes you want to know what is the “profile id” of this user without loading the whole profile for this user.To do this you just need to add another property to your entity with @Columnnamed exactly as the column created by your relation. Example:

  1. import {Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn} from "typeorm";
  2. import {Profile} from "./Profile";
  3. @Entity()
  4. export class User {
  5. @PrimaryGeneratedColumn()
  6. id: number;
  7. @Column()
  8. name: string;
  9. @Column({ nullable: true })
  10. profileId: number;
  11. @OneToOne(type => Profile)
  12. @JoinColumn()
  13. profile: Profile;
  14. }

That’s all. Next time you load a user object it will contain a profile id:

  1. User {
  2. id: 1,
  3. name: "Umed",
  4. profileId: 1
  5. }

How to load relations in entities

The easiest way to load your entity relations is to use relations option in FindOptions:

  1. const users = await connection.getRepository(User).find({ relations: ["profile", "photos", "videos"] });

Alternative and more flexible way is to use QueryBuilder:

  1. const user = await connection
  2. .getRepository(User)
  3. .createQueryBuilder("user")
  4. .leftJoinAndSelect("user.profile", "profile")
  5. .leftJoinAndSelect("user.photos", "photo")
  6. .leftJoinAndSelect("user.videos", "video")
  7. .getMany();

Using QueryBuilder you can do innerJoinAndSelect instead of leftJoinAndSelect(to learn the difference between LEFT JOIN and INNER JOIN refer to your SQL documentation),you can join relation data by a condition, make ordering, etc.

Learn more about QueryBuilder.

Avoid relation property initializers

Sometimes it is useful to initialize your relation properties, for example:

  1. import {Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable} from "typeorm";
  2. import {Category} from "./Category";
  3. @Entity()
  4. export class Question {
  5. @PrimaryGeneratedColumn()
  6. id: number;
  7. @Column()
  8. title: string;
  9. @Column()
  10. text: string;
  11. @ManyToMany(type => Category, category => category.questions)
  12. @JoinTable()
  13. categories: Category[] = []; // see = [] initialization here
  14. }

However in TypeORM entities it may cause problems.To understand the problem, let’s first try to load a Question entity WITHOUT the initializer set.When you load a question it will return an object like this:

  1. Question {
  2. id: 1,
  3. title: "Question about ..."
  4. }

Now when you save this object categories inside it won’t be touched - because it is unset.

But if you have initializer, the loaded object will look like as follow:

  1. Question {
  2. id: 1,
  3. title: "Question about ...",
  4. categories: []
  5. }

When you save the object it will check if there are any categories in the database bind to the question -and it will detach all of them. Why? Because relation equal to [] or any items inside it will be consideredlike something was removed from it, there is no other way to check if an object was removed from entity or not.

Therefore, saving an object like this will bring you problems - it will remove all previously set categories.

How to avoid this behaviour? Simply don’t initialize arrays in your entities.Same rule applies to a constructor - don’t initialize it in a constructor as well.