Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

327
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda