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

330
Views
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
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!