Akash Sourav Nayak
Akash Sourav Nayak

Reputation: 203

Associate different models using sequelize?

Hi I am trying to associate my User model with login model and Question_details models.But if i am using the Question_details association then i am geeting eagerLodingError :user is not associated to login but if i am commenting it then it works fine so how can i associate it ?

But if i am associating with

User Model

    module.exports = (sequelize, DataTypes) => {
        var Users = sequelize.define('users', {
            name: {
                type: DataTypes.STRING(100)
             }
            phone: {
                type: DataTypes.BIGINT,
                unique: true
            }
        }, { freezeTableName: true });

        Users.associate = function(models) {
            Users.hasOne(models.login, {
                foreignKey: 'user_id',
                as: 'loginDetails'
            });
        };

        Users.associate = function(models) {
            Users.hasMany(models.customer_query, {
                foreignKey: 'user_id',
                as: 'queryDetails'
            });
        };

        return Users;
    };

LOGIN MODEL

module.exports = (sequelize, DataTypes) => {
    var Login = sequelize.define('login', {
        user_id: {
            type: DataTypes.INTEGER
        },
        user_name: {
            type: DataTypes.STRING(500),
            isEmail: true
        },
        password: {
            type: DataTypes.STRING(500)
        },
        role_id: {
            type: DataTypes.INTEGER
        }
    }, {
        underscored: true,
        freezeTableName: true
    });

    Login.associate = function(models) {
        Login.belongsTo(models.users, {
            foreignKey: 'user_id',
            onDelete: 'CASCADE'
        });
    };
    Login.associate = function(models) {
        Login.belongsTo(models.roles, {
            foreignKey: 'role_id',
            onDelete: 'CASCADE'
        });
    };
    return Login;

};

questionDetails Model

    module.exports = function(sequelize, DataTypes) {
        var questionDetails = sequelize.define('question_details', {
            query_id: {
                type: DataTypes.INTEGER
            },
            ques_type_id: {
                type: DataTypes.INTEGER
            },
            created_by: {
                type: DataTypes.INTEGER
            },
            question: {
                type: DataTypes.TEXT
            },

        }, { freezeTableName: true });

 questionDetails.associate = function(models) {
            questionDetails.belongsTo(models.users, {
                foreignKey: 'created_by',
                onDelete: 'CASCADE'
            });
        };

        return questionDetails;
    };

Upvotes: 4

Views: 20746

Answers (1)

Dylan Aspden
Dylan Aspden

Reputation: 1692

You only have to define associate once. When you define it the second time you're actually overwriting it. So for the User model you should actually do...

    Users.associate = function(models) {
      Users.hasOne(models.login, {
        foreignKey: 'user_id',
        as: 'loginDetails'
      });

      Users.hasMany(models.customer_query, {
        foreignKey: 'user_id',
        as: 'queryDetails'
      });
    };

Do similarly for your login model as you are also overwriting the associate function there.

Good luck! :)

Upvotes: 17

Related Questions