Ashutosh
Ashutosh

Reputation: 4675

nodejs sequelize loading nested models

My mistake, question was incomplete, marking all missing text in bold

I have following models:


I'm trying to read patient and want to read address as well which looks like:

Patient => PatientAddress => Address

I also need to read Personal model with Patient

I can read PatientAddress, Personal using include but not able to read Address. I tried these:

return Patient.findAll({
    limit: limit,
    offset: offset,
    include: [{
        model: [Personal, PatientAddress],
        include: [{
            Address
        }]
    }]
});

return Patient.findAll({
    limit: limit,
    offset: offset,
    include: [{
        model: [Personal, PatientAddress],
        include: [{
            PatientAddress.Address
        }]
    }]
});

or

return Patient.findAll({
    limit: limit,
    offset: offset,
    include: [Personal, PatientAddress, PatientAddress.Address]
});

What to change?

Upvotes: 0

Views: 388

Answers (2)

simon.ro
simon.ro

Reputation: 3302

Personal is directly related to Patient but Address belongs to PatientAddress. Therefore you have to place the Address-include inside the include for PatientAddress, while Personalstays a first-level include.

return Patient.findAll({
    limit: limit,
    offset: offset,
    include: [
        {
            model: PatientAddress,
            include: [Address]
        },
        {
            model: Personal
        }
    ]
});

Upvotes: 1

Shaharyar
Shaharyar

Reputation: 12439

Your first query was pretty close, try this one:

return Patient.findAll({
    limit: limit,
    offset: offset,
    include: [
        {
            model: PatientAddress,
            include: [Address]
        }
    ]
});

Upvotes: 1

Related Questions