One-to-one relations
One-to-one is a relation where A contains only one instance of B, and B contains only one instance of A. Let's take for example User and Profile entities. User can have only a single profile, and a single profile is owned by only a single user.
1
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
2
​
3
@Entity()
4
export class Profile {
5
​
6
@PrimaryGeneratedColumn()
7
id: number;
8
​
9
@Column()
10
gender: string;
11
​
12
@Column()
13
photo: string;
14
​
15
}
Copied!
1
import {Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn} from "typeorm";
2
import {Profile} from "./Profile";
3
​
4
@Entity()
5
export class User {
6
​
7
@PrimaryGeneratedColumn()
8
id: number;
9
​
10
@Column()
11
name: string;
12
​
13
@OneToOne(() => Profile)
14
@JoinColumn()
15
profile: Profile;
16
​
17
}
Copied!
Here we added @OneToOne to the profile and specify the target relation type to be Profile. We also added @JoinColumn which is required and must be set only on one side of the relation. The side you set @JoinColumn on, that side's table will contain a "relation id" and foreign keys to target entity table.
This example will produce following tables:
1
+-------------+--------------+----------------------------+
2
| profile |
3
+-------------+--------------+----------------------------+
4
| id | int(11) | PRIMARY KEY AUTO_INCREMENT |
5
| gender | varchar(255) | |
6
| photo | varchar(255) | |
7
+-------------+--------------+----------------------------+
8
​
9
+-------------+--------------+----------------------------+
10
| user |
11
+-------------+--------------+----------------------------+
12
| id | int(11) | PRIMARY KEY AUTO_INCREMENT |
13
| name | varchar(255) | |
14
| profileId | int(11) | FOREIGN KEY |
15
+-------------+--------------+----------------------------+
Copied!
Again, @JoinColumn must be set only on one side of relation - the side that must have the foreign key in the database table.
Example how to save such a relation:
1
const profile = new Profile();
2
profile.gender = "male";
3
profile.photo = "me.jpg";
4
await connection.manager.save(profile);
5
​
6
const user = new User();
7
user.name = 'Joe Smith';
8
user.profile = profile;
9
await connection.manager.save(user);
Copied!
With cascades enabled you can save this relation with only one save call.
To load user with profile inside you must specify relation in FindOptions:
1
const userRepository = connection.getRepository(User);
2
const users = await userRepository.find({ relations: ["profile"] });
Copied!
Or using QueryBuilder you can join them:
1
const users = await connection
2
.getRepository(User)
3
.createQueryBuilder("user")
4
.leftJoinAndSelect("user.profile", "profile")
5
.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.
Relations can be uni-directional and bi-directional. Uni-directional are relations with a relation decorator only on one side. Bi-directional are relations with decorators on both sides of a relation.
We just created a uni-directional relation. Let's make it bi-directional:
1
import {Entity, PrimaryGeneratedColumn, Column, OneToOne} from "typeorm";
2
import {User} from "./User";
3
​
4
@Entity()
5
export class Profile {
6
​
7
@PrimaryGeneratedColumn()
8
id: number;
9
​
10
@Column()
11
gender: string;
12
​
13
@Column()
14
photo: string;
15
​
16
@OneToOne(() => User, user => user.profile) // specify inverse side as a second parameter
17
user: User;
18
​
19
}
Copied!
1
import {Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn} from "typeorm";
2
import {Profile} from "./Profile";
3
​
4
@Entity()
5
export class User {
6
​
7
@PrimaryGeneratedColumn()
8
id: number;
9
​
10
@Column()
11
name: string;
12
​
13
@OneToOne(() => Profile, profile => profile.user) // specify inverse side as a second parameter
14
@JoinColumn()
15
profile: Profile;
16
​
17
}
Copied!
We just made our relation bi-directional. Note, inverse relation does not have a @JoinColumn. @JoinColumn must only be on one side of the relation - on the table that will own the foreign key.
Bi-directional relations allow you to join relations from both sides using QueryBuilder:
1
const profiles = await connection
2
.getRepository(Profile)
3
.createQueryBuilder("profile")
4
.leftJoinAndSelect("profile.user", "user")
5
.getMany();
Copied!
Copy link