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

I am receiving the error when I make a call to my API with a get request:

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

My Models look like this:

module.exports = (sequelize, Sequelize) => {
    const Productions = sequelize.define("productions", {
        id: {
            type: Sequelize.SMALLINT,
            autoIncrement: true,
            primaryKey: true
        },
        setupTime: {
            type: Sequelize.DECIMAL(6, 3)
        },
        notes: {
            type: Sequelize.TEXT
        }
    }, { timestamps: false });

    return Productions;
};
module.exports = (sequelize, Sequelize) => {
    const ProductionPrints = sequelize.define("productionPrints", {
        id: {
            type: Sequelize.SMALLINT,
            autoIncrement: true,
            primaryKey: true
        },
        compDate: {
            type: Sequelize.DATE
        }
    }, { timestamps: false });

    return ProductionPrints;
};

The relationship between the models is defined here:

db.productions = require("./productions.model.js")(sequelize, Sequelize);
db.productionprints = require("./production-prints.model.js")(sequelize, Sequelize);

db.productions.hasOne(db.productionprints, {
    foreignKey: {
        name: 'productionId',
        allowNull: false
    }
});
db.productionprints.belongsTo(db.productions, { foreignKey: 'productionId' });

And the sequelize query looks as so:

const db = require("../models");
const Productions = db.productions;
const ProductionPrints = db.productionPrints;

exports.findAll = (req, res) => {
    Productions.findAll({
        include: [ { model: ProductionPrints, as: 'prints' } ]
    })
        .then(data => {
            res.send(data);
        })
        .catch(err => {
            res.status(500).send({
                message:
                    err.message || "An error occurred while finding the productions."
            });
        });
};

I have checked around for others with the issue but have had no avail with any solutions posted on those problems. Generally it was caused by typos, or error in the require paths. I have checked those and all my other includes work, just not on any of the models I include on the productions model.

Any feedback is greatly appreciated.



Solution 1:[1]

I had the same issue , this is usually caused by naming issue , to track the issue you can check one of the following places to resolve it

  1. check if you are calling the correct model class name
  2. when importing models becarefull not to call the file name instead of model name => the one exported

3.check if you got your association correctly by calling the exported model not the file name

  1. check if your cases e.g users vs Users.

a bonus tip is to use same name for model and file name to avoid these issues because the moment you make them different you likely to make these mistakes

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kelvin nyadzayo