示例

一对一关联

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;
 
  @Column()
  name: string;
 
  @OneToOne(() => Profile, profile => profile.user)
  profile: Profile;
}
 
@Entity()
export class Profile {
  @PrimaryGeneratedColumn()
  id: number;
 
  @Column()
  bio: string;
 
  @OneToOne(() => User, user => user.profile)
  user: User;
}

一对多关联

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;
 
  @Column()
  name: string;
 
	@OneToMany(() => Post, post => post.user)
	posts: Post[];
}
 
@Entity()
export class Post {
	@PrimaryGeneratedColumn()
	id: number;
 
	@Column()
	title: string;
 
	@ManyToOne(() => User, user => user.posts)
	user: User;
}

多对多关联

@Entity()
export class Author {
	@PrimaryGeneratedColumn()
	id: number;
 
	@Column()
	name: string;
 
	@ManyToMany(() => Book, book => book.authors)
	books: Book[];
}
 
@Entity()
export class Book {
	@PrimaryGeneratedColumn()
	id: number;
 
	@Column()
	title: string;
 
	@ManyToMany(() => Author, author => author.books)
	authors: Author[];
}