Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda