4

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;
    };
Akash Sourav Nayak
  • 203
  • 1
  • 8
  • 21

1 Answers1

17

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! :)

Dylan Aspden
  • 1,692
  • 13
  • 14
  • 7
    I cannot find association function or any syntax like Model.associate = function(models) { any where in the sequelize docs. Can any one please help me to find details about association function in docs or anywhere. – Abhishek Kumar Sep 01 '18 at 07:23
  • @AbhishekKumar `Users.associate` is a custom function that we created to add these relationships to the model. We had to do it that way because we need to call the `associate` function after all of the models have been imported into Sequelize. See https://github.com/sequelize/express-example/blob/master/models/index.js for an example. – Dylan Aspden Sep 01 '18 at 20:41
  • 1
    my question almost same, but little nested, https://jsfiddle.net/j74rt9y1/1/ the second assosiation didn't works and no error,, anyone can helps ? – Budi Mulyo May 02 '19 at 05:41
  • @DylanAspden How to create a login record for a user? – prit.patel Jul 11 '19 at 12:09
  • here is the complete explaination how its done https://codebysamgan.com/how-to-create-model-association-in-sequelize-express-js-node-js – Mohammed Samgan Khan Mar 24 '21 at 14:27
  • Here in 2023 and there is still no documentation for the association function – Jess Feb 21 '23 at 14:37