sequelize: Error: Include unexpected. Element has to be either a Model, an Association or an object.

Issue Description

I am trying to make a big query with many joins and it seems sequelize doesn’t like the way I am doing this.

What are you doing?

const ids = [/*Some integer here*/];
models.Exercise.findAll({
  // no need for that part here
  attributes: [
    "id",
    "title",
    "description",
    "version",
    "createdAt",
    "updatedAt"
  ],
  where: {
    id: {
      [Op.in]: ids
    }
  },
  include: [
    // load exercise evaluation
    {
      models: models.Exercise_Metrics,
      as: "metrics",
      attributes: [
        ["vote_count", "votes"],
        ["avg_vote_score", "avg_vote"]
      ]
    },
    // load tags linked to this exercise ( with their category included )
    {
      models: models.Exercise_Tag,
      as: "tags",
      attributes: [],
      include: [
        {
          models: models.Tag,
          attributes: ["id", "text"],
          include: [
            {
              models: models.Tag_Category,
              as: "category",
              attributes: [
                ["kind", "category"],
                ["id", "category_id"]
              ]
            }
          ]
        }
      ]
    }
  ]
});

What do you expect to happen?

I expect that to work when I use findAll

What is actually happening?

Error: Include unexpected. Element has to be either a Model, an Association or an object.
    at Function._conformInclude (D:\workspaceshit\exercises_library\node_modules\sequelize\lib\model.js:390:11)
    at options.include.options.include.map.include (D:\workspaceshit\exercises_library\node_modules\sequelize\lib\model.js:326:59)
    at Array.map (<anonymous>)
    at Function._conformIncludes (D:\workspaceshit\exercises_library\node_modules\sequelize\lib\model.js:326:39)
    at Function._baseMerge (D:\workspaceshit\exercises_library\node_modules\sequelize\lib\model.js:791:10)
    at Function._defaultsOptions (D:\workspaceshit\exercises_library\node_modules\sequelize\lib\model.js:828:17)
    at Function._injectScope (D:\workspaceshit\exercises_library\node_modules\sequelize\lib\model.js:3288:10)
    at Promise.try (D:\workspaceshit\exercises_library\node_modules\sequelize\lib\model.js:1707:12)
    at tryCatcher (D:\workspaceshit\exercises_library\node_modules\bluebird\js\release\util.js:16:23)
    at Function.Promise.attempt.Promise.try (D:\workspaceshit\exercises_library\node_modules\bluebird\js\release\method.js:39:29)
    at Function.findAll (D:\workspaceshit\exercises_library\node_modules\sequelize\lib\model.js:1706:23)

Additional context

Here are my models definitions if It could help you : Exercise Exercise_Metrics Exercise_Tag Tag Tag_Category

Environment

  • Sequelize version: 5.21.2
  • Node.js version: v10.16.0
  • Operating System: Windows 10

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don’t know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don’t know how to start, I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 72 (1 by maintainers)

Most upvoted comments

I made a typo : models instead of model Sorry for the issue

I had the issue but it was because i did not use the proper name for the model name use that i defined it as

in my model i defined it as sequelize.define(‘houseLocation’

and in query i had lower “l”

include: [
          {
            model: db.houselocation,
            required: true
          }
        ],

Once i used the correct case as I had in the in model then it work. Check the name you gave it in the define bit in the model and check your case

include: [
          {
            model: db.houseLocation,
            required: true
          }
        ],