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.
Here we added @OneToOne to the user and specified 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 the target entity table.
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:
import { Entity, PrimaryGeneratedColumn, Column, OneToOne } from"typeorm"import { User } from"./User"@Entity()exportclassProfile { @PrimaryGeneratedColumn() id:number @Column() gender:string @Column() photo:string @OneToOne(() => User, (user) =>user.profile) // specify inverse side as a second parameter user:User}
import { Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn,} from"typeorm"import { Profile } from"./Profile"@Entity()exportclassUser { @PrimaryGeneratedColumn() id:number @Column() name:string @OneToOne(() => Profile, (profile) =>profile.user) // specify inverse side as a second parameter @JoinColumn() profile:Profile}
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: