Categories
Mastering Development

SEQUELIZE: belongsTo called with something that’s not a subclass of Sequelize.Model

I’m newbie with sequelize and I’m trying to assiacite two tables. Users and Projects. I have defined a model for each one called UserModel and ProjectModel and when I try to associate projects with user I’ve got this error:

belongsTo called with something that’s not a subclass of
Sequelize.Model

These are the files where I define each model:

user.js

module.exports = (sequelize, DataType) => {
    const UserModel = sequelize.define("user", {
        id: {
            type: DataType.INTEGER,
            primaryKey: true,
            autoIncrement: true,
            allowNull: false,
        },
        email: {
            type: DataType.STRING,
            allowNull: false,
            isEmail: {
                msg: "The format of the e-mail is not correct"
            },
            validate: {
                notNull: {
                    msg: "E-mail cannot be empty"
                }
            }
        },
        name: {
            type: DataType.STRING,
            is: /^[a-zA-Z ]+$/i,
            allowNull: false,
            validate: {
                notNull: {
                    msg: "Name cannot be empty"
                }
            }
        },
        surname: {
            type: DataType.STRING,
            is: /^[a-zA-Z ]+$/i,
            allowNull: false,
            validate: {
                notNull: {
                    msg: "Surname cannot be empty"
                }
            }
        }
    });
    UserModel.associate = (models) => {
        UserModel.hasMany(models.ProjectModel, {
            foreignKey: "userID"
        })
    }

    return UserModel;    
};

project.js

module.exports = (sequelize, DataType) => {
    const ProjectModel =  sequelize.define("project", {
        id: {
            type: DataType.INTEGER,
            primaryKey: true,
            autoIncrement: true,
            allowNull: false,
        },
        name: {
            type: DataType.STRING,
            is: /^[a-zA-Z ]+$/i,
            allowNull: false,
            validate: {
                notNull: {
                    msg: "Name cannot be empty"
                }
            }
        },
        body: {
            type: DataType.TEXT,
            allowNull: false,
            validate: {
                notNull: {
                    msg: "Body cannot be empty"
                }
            }
        },
        status: {
            type: DataType.ENUM("active", "inactive", "declined", "completed"),
            allowNull: false,
            validate: {
                notNull: {
                    msg: "Status cannot be empty"
                }
            }
        },
        userID: {
            type: DataType.INTEGER,
            allowNull: false,
            validate: {
                notNull: {
                    msg: "userID cannot be empty"
                }
            },
            references: {
                model: UserModel,
                key: "id"
            }            
        }
    }); 
    ProjectModel.associate = (models) => {
        ProjectModel.belongsTo(models.UserModel, {
            foreignKey: "userID"
        });
    }

    ProjectModel.belongsTo(UserModel, {
        foreignKey: "userID"
    });


    return ProjectModel;
}

What am I doing wrong?

Leave a Reply

Your email address will not be published. Required fields are marked *