Search code examples
nestjstypeorm

NestJS TypeORM OneToMany column not displayed in db


I've setup a OnetoMany relationship between table "Movie" and "Like". And a ManyToOne relationship between table "Like" and "Movie". In the like table I can see that there's a column created 'movieID' that links the row to a movie just as expected. However there's no "likes" column created in the Movie table that should indicate the relationship.

import { Like } from './like.entity';
@Entity()
export class Movie {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @OneToMany(() => Like, (like) => like.movie)
  likes: Like[];
}
import { Movie } from './movie.entity';

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

  @Column({ default: 0 })
  likes: number;

  @ManyToOne(() => Movie, (movie) => movie.likes)
  movie: Movie;
}

Solution

  • A OneToMany/ManyToOne relationship only needs an entry in the table representing the Many part. It is sufficient to know which likes belong to a movie in order to deduce which likes a movie has. Maintaining the information in both tables would be inefficient and error-prone. The behavior is as expected from an ORM.