Skip to content

Completely confused on how to eager load through table data. #10989

Closed

Description

@csalvato

What was unclear/insufficient/not covered in the documentation

Thank you so much for putting together a comprehensive SQL ORM!

I am desperately trying to figure out how to load data from my through table on eager load. Here's an example of a kids skill-class tracking program I'm working on:

const parent = await models.User.findOne({
      where: { email: req.user.email },
      attributes: ['email', 'firstName', 'lastName', 'phone'],
      include: [
        {
          model: models.Student,
          include: [
            {
              model: models.Program,
              through: {
                model: models.StudentProgram
              }
            }
          ]
        }
      ]
    })

This works totally fine, and creates something that will respond to this:

parent.Students[0].Programs[0].StudentProgram

Wonderful. The documentation got me this far...

But my StudentProgram model has an association that I want to access:

StudentProgram.SkillLevels = StudentProgram.belongsToMany(
      models.SkillLevel,
      {
        through: models.ProgramSkillLevel
      }
    )

I can get this data by running this code:

programSkillLevels = await parent.Students[0].Programs[0].StudentProgram.getSkillLevels()

But that doesn't cut it, because I have something called ProgramSkills which is nested within a ProgramSkillLevel...

I am hoping that I can do something like this:

const parent = await models.User.findOne({
      where: { email: req.user.email },
      attributes: ['email', 'firstName', 'lastName', 'phone'],
      // include: [{ all: true, nested: true }]
      include: [
        {
          model: models.Student,
          include: [
            {
              model: models.Program,
              through: {
                model: models.StudentProgram,
                include: [
                  {
                    model: models.SkillLevel,
                    through: {
                      model: models.StudentProgram
                    },
                    include: [
                      {
                        model: models.Skills,
                        through: { model: models.ProgramSkillLevel }
                      }
                    ]
                  }
                ]
              }
            }
          ]
        }
      ]
    })

But this produces an error.

TBH I have had a lot of struggles with the documentation around BelongsToMany relationships and through tables. My app is pretty reliant on many through tables, which has made development incredibly slow, where I was hoping Sequelize would speed up dev :(

If possible: Provide some suggestion how we can enhance the docs

In order for me to provide a concrete suggestion, I need to understand how to do this first, or if it's at all possible. I have a feeling this is possible, because accessing the same data with a getter is working just fine...I just have no idea how to nest this data on load.

Activity

  1. added
    docs
    on May 22, 2019
  2. papb commented on Jul 25, 2019

    @papb
    Member

    Hello, I will try to improve the docs on this later. It is indeed possible! But I don't have much time to explain right now - it's not so simple (if I understood you correctly).

    Please wait just a bit more! We are happy that you are thankful for Sequelize.

  3. self-assigned this
    on Jul 25, 2019
  4. added
    status: understood
    on Jul 25, 2019
  5. added
    status: wip
    and removed
    status: understood
    on Sep 6, 2019
  6. removed
    status: wip
    on Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

docs

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions