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

335
Visualizações
How to use JSON Array as foreign key to the same table? (Sequelize+ Postgres)

I have the following table:

id name brothers
1 John [3, 4]
2 Ben []
3 Mary [1, 4]
4 Peter [1, 3]

How to set up a relation where I could get all brothers data in single query on single record? the brothers column is using Postgres Array type.

Right now I am using 2 queries, 1 to get John, and then query again to get brothers info based on his brothers IDs.

I found the following in Sequelize Docs:

Sequelize supports self-referential Many-to-Many relationships, intuitively:

Person.belongsToMany(Person, { as: 'Children', through: 'PersonChildren' })
// This will create the table PersonChildren which stores the ids of the objects.

But I couldn't get it work with:

People.belongsToMany(People, { as: 'Brothers', through: 'peopleBrothers', foreignKey: 'brothers' })

Can this be done?

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