Sequelize Assosication called with something that's not a subclass of Sequelize.Model

Putting A.hasOne(B) and B.belongsTo(A) in the same file solved the issue for me.


You need to add your associations in a function called associate(models). The models argument contains all your existing Model definitions keyed by their definition name ("user_tm" in this case).

var User = sequelize.define('user_tm', {
  // ... user_tm definition
});

var UserEmployee = sequelize.define('user_employee_tm', {
  // ... user_employee_tm definition
});

UserEmployee.associate = (models) => {
  UserEmployee.belongsTo(models.user_tm, {foreignKey: 'ID', as: 'Employee'});
};

Turns out I've found that I just need to define my Object of UserEmployee Here's the code that I've Fixed

const sequelize = require('../config/connectionDatabase');
var Sequelize = require('sequelize');
const User = require('../models/user');
const Company = require('../models/company');

var UserEmployee = sequelize.define('user_employee_tm', {
    DateJoin: {
        type: Sequelize.DATE
    },
    UserID: {
        type: Sequelize.INTEGER,
        references: {
            model: User,
            key: "UserID"
        }
    },
    CompanyID: {
        type: Sequelize.INTEGER,
        references: {
            model: Company,
            key: "CompanyID"
        }
    }
});
UserEmployee.belongsTo(Company, {as: 'Company', foreignKey: 'CompanyID'});
UserEmployee.belongsTo(User, {as: 'User', foreignKey: 'UserID'});
module.exports = UserEmployee;

no need to set as associate due to Sequelize has set method associate them, and I've also fix the relation of it.

hope others that has same problem with me could look after it, without make 2 models ends up on 1 file.

P.S. Thanks for doublesharp for your help to point my wrong doing