0
0
NestJSframework~10 mins

Relations (OneToMany, ManyToOne, ManyToMany) in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a OneToMany relation from User to Post.

NestJS
import { Entity, PrimaryGeneratedColumn, Column, [1] } from 'typeorm';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @[1](() => Post, post => post.user)
  posts: Post[];
}
Drag options to blanks, or click blank then click option'
AJoinColumn
BManyToOne
CManyToMany
DOneToMany
Attempts:
3 left
💡 Hint
Common Mistakes
Using ManyToOne instead of OneToMany here.
Forgetting to import the decorator.
2fill in blank
medium

Complete the code to define the ManyToOne side of the relation from Post to User.

NestJS
import { Entity, PrimaryGeneratedColumn, Column, [1] } from 'typeorm';

@Entity()
export class Post {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @[1](() => User, user => user.posts)
  user: User;
}
Drag options to blanks, or click blank then click option'
AManyToOne
BOneToMany
CManyToMany
DJoinTable
Attempts:
3 left
💡 Hint
Common Mistakes
Using OneToMany on the Post side.
Confusing JoinTable with JoinColumn.
3fill in blank
hard

Fix the error in the ManyToMany relation between Student and Course by completing the decorator.

NestJS
import { Entity, PrimaryGeneratedColumn, Column, [1] } from 'typeorm';

@Entity()
export class Student {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @[1](() => Course, course => course.students)
  courses: Course[];
}
Drag options to blanks, or click blank then click option'
AOneToMany
BManyToMany
CManyToOne
DJoinColumn
Attempts:
3 left
💡 Hint
Common Mistakes
Using OneToMany or ManyToOne instead of ManyToMany.
Forgetting to add JoinTable on one side.
4fill in blank
hard

Fill both blanks to complete the ManyToMany relation with JoinTable between Course and Student.

NestJS
import { Entity, PrimaryGeneratedColumn, Column, [1], [2] } from 'typeorm';

@Entity()
export class Course {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @ManyToMany(() => Student, student => student.courses)
  @[2]()
  students: Student[];
}
Drag options to blanks, or click blank then click option'
AManyToMany
BJoinTable
COneToMany
DJoinColumn
Attempts:
3 left
💡 Hint
Common Mistakes
Using JoinColumn instead of JoinTable.
Not importing both decorators.
5fill in blank
hard

Fill all three blanks to create a OneToMany and ManyToOne relation between Author and Book with proper imports and decorators.

NestJS
import { Entity, PrimaryGeneratedColumn, Column, [1], [2], [3] } from 'typeorm';

@Entity()
export class Author {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @[2](() => Book, book => book.author)
  books: Book[];
}

@Entity()
export class Book {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @[3](() => Author, author => author.books)
  author: Author;
}
Drag options to blanks, or click blank then click option'
AOneToMany
BManyToOne
CJoinColumn
DManyToMany
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up OneToMany and ManyToOne decorators.
Forgetting to import all needed decorators.