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

151
Views
TypeORM Entity relation with itself

I have a entity called "Comments", the idea is having replies for these comments. I can have also replies for replies. Example as following:

-Comment1
---Reply 1 Of Comment 1
-----Reply Of Reply 1
---Reply 2 Of Comment 1
-----Reply1 Of Reply 2
-------Reply1 Of Reply 1 of Reply 2
-Comment2

The entity is this one:

@Entity('comments') {
    @PrimaryGenerated()
    id: string;

    @Column()
    message: string;

    @Column()
    date: Date;
}

The approach I was thinking of is to have a "self relationship" where I could create another field maybe called "parent_id" and where I have this field filled, I consider as a reply. Any ideas of how I can do this "self relationship" or ideas of another approach?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This is an example of self referencing relation:

@Entity('comments')
export class Comment {
    @PrimaryGenerated()
    id: string;

    @Column()
    message: string;

    @Column()
    date: Date;

    @Column()
    parentId: number;

    @ManyToOne(type => Comment, comment => comment.children)
    @JoinColumn({ name: "parentId" })
    parent: Comment;

    @OneToMany(type => Category, comment => comment.parent)
    children: Comment[];
}

Also you can look at Tree Entities as another approach.

about 4 years ago · Juan Pablo Isaza Report
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!