Many-to-one / one-to-many relations
Many-to-one / one-to-many is a relation where A contains multiple instances of B, but B contains only one instance of A. Let's take for example User and Photo entities. User can have multiple photos, but each photo is owned by only one single user.
1
import {Entity, PrimaryGeneratedColumn, Column, ManyToOne} from "typeorm";
2
import {User} from "./User";
3
​
4
@Entity()
5
export class Photo {
6
​
7
@PrimaryGeneratedColumn()
8
id: number;
9
​
10
@Column()
11
url: string;
12
​
13
@ManyToOne(() => User, user => user.photos)
14
user: User;
15
​
16
}
Copied!
1
import {Entity, PrimaryGeneratedColumn, Column, OneToMany} from "typeorm";
2
import {Photo} from "./Photo";
3
​
4
@Entity()
5
export class User {
6
​
7
@PrimaryGeneratedColumn()
8
id: number;
9
​
10
@Column()
11
name: string;
12
​
13
@OneToMany(() => Photo, photo => photo.user)
14
photos: Photo[];
15
​
16
}
Copied!
Here we added @OneToMany to the photos property and specified the target relation type to be Photo. You can omit @JoinColumn in a @ManyToOne / @OneToMany relation. @OneToMany cannot exist without @ManyToOne. If you want to use @OneToMany, @ManyToOne is required. However, the inverse is not required: If you only care about the @ManyToOne relationship, you can define it without having @OneToMany on the related entity. Where you set @ManyToOne - its related entity will have "relation id" and foreign key.
This example will produce following tables:
1
+-------------+--------------+----------------------------+
2
| photo |
3
+-------------+--------------+----------------------------+
4
| id | int(11) | PRIMARY KEY AUTO_INCREMENT |
5
| url | varchar(255) | |
6
| userId | int(11) | FOREIGN KEY |
7
+-------------+--------------+----------------------------+
8
​
9
+-------------+--------------+----------------------------+
10
| user |
11
+-------------+--------------+----------------------------+
12
| id | int(11) | PRIMARY KEY AUTO_INCREMENT |
13
| name | varchar(255) | |
14
+-------------+--------------+----------------------------+
Copied!
Example how to save such relation:
1
const photo1 = new Photo();
2
photo1.url = "me.jpg";
3
await connection.manager.save(photo1);
4
​
5
const photo2 = new Photo();
6
photo2.url = "me-and-bears.jpg";
7
await connection.manager.save(photo2);
8
​
9
const user = new User();
10
user.name = "John";
11
user.photos = [photo1, photo2];
12
await connection.manager.save(user);
Copied!
or alternatively you can do:
1
const user = new User();
2
user.name = "Leo";
3
await connection.manager.save(user);
4
​
5
const photo1 = new Photo();
6
photo1.url = "me.jpg";
7
photo1.user = user;
8
await connection.manager.save(photo1);
9
​
10
const photo2 = new Photo();
11
photo2.url = "me-and-bears.jpg";
12
photo2.user = user;
13
await connection.manager.save(photo2);
Copied!
With cascades enabled you can save this relation with only one save call.
To load a user with photos inside you must specify the relation in FindOptions:
1
const userRepository = connection.getRepository(User);
2
const users = await userRepository.find({ relations: ["photos"] });
3
​
4
// or from inverse side
5
​
6
const photoRepository = connection.getRepository(Photo);
7
const photos = await photoRepository.find({ relations: ["user"] });
Copied!
Or using QueryBuilder you can join them:
1
const users = await connection
2
.getRepository(User)
3
.createQueryBuilder("user")
4
.leftJoinAndSelect("user.photos", "photo")
5
.getMany();
6
​
7
// or from inverse side
8
​
9
const photos = await connection
10
.getRepository(Photo)
11
.createQueryBuilder("photo")
12
.leftJoinAndSelect("photo.user", "user")
13
.getMany();
Copied!
With eager loading enabled on a relation, you don't have to specify relations in the find command as it will ALWAYS be loaded automatically. If you use QueryBuilder eager relations are disabled, you have to use leftJoinAndSelect to load the relation.
Copy link