Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

399
Views
Union type in typeorm relation for nested comments

I am trying to create kind of CRUD-blog with posts, users and comments. And would like to have 2-level comments like ones on Youtube: top-level comments are attached to video/post and replies are attached to top-level comments. So top-level comments have post as their parent and replies have top-level comment as their parent.

Does it meant that I have to use different entity classes(and thus different tables) for top-level comments and replies? Can typeorm handle union type as parent?

@Entity()
export class Comment extends CreatableEntity {

  @Column({ type: 'text' })
  content: string

  @ManyToOne(type => User, user => user.comments, {
    nullable: false,
  })
  author: User

  @ManyToOne(type => Post | Comment, parent => parentcomments, {
    nullable: false,
  })
  parent: Post | Comment

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!