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

170
Views
Sequelize returns empty model from certain nested level

I've got a model I'm trying to query and I'm trying to include some very nested models (which their relation varies between hasOne to hasMany) to it, yet it seems that from a certain level the object comes back and an empty model

const parent = Parent.findByPk(id, {include: ["anotherChild", 
    {model: Child1, as: "child1", include: [
            {model: Child2, as: "children2", include: [
                {model: Child3, as: "child3", include: [
                    {model: Child4, as: "children4", include: [
                        {model: Child5, as: "children5"}
                    ]}
                ]}
            ]}
        ]}
    ]});

When I try to print the most nested child, I receive a model instance but with no values (there are matching results, otherwise it would also just set them as null)

console.log(parent.child1.children2[0].child3.children4[0].children5[0]);
// prints:
// Child5 {
//   dataValues: { '': null },
//   _previousDataValues: { '': null },
//   ....
// }

when I try to separate the query it does work

const parent = Parent.findByPk(id, {include: ["anotherChild", {model: Child1, as: "child1"}]});

const fromChild1 = Child1.findByPk(parent.child1.id, include: [
            {model: Child2, as: "children2", include: [
                {model: Child3, as: "child3", include: [
                    {model: Child4, as: "children4", include: [
                        {model: Child5, as: "children5"}
                    ]}
                ]}
            ]}
        ]
   );

result in the following value:

console.log(fromChild1.children2[0].child3.children4[0].children5[0]);
// prints:
// Child5 {
//   dataValues: { 'realKey': 'realValue', 'realData': 'someData'... },
//   _previousDataValues: { 'realKey': 'realValue, 'realData': 'someData'... },
//   ....
// }

Why is that, and how could I get over that? (using {all: true, nested: true} is not possible due to belongsTo / belongsToMany relationships between them)

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!