Associate different models using sequelize?

14,826

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

Share:
14,826
Akash Sourav Nayak
Author by

Akash Sourav Nayak

Experienced Full-stack Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Node.js,Java,Kotlin, ReactJs,Spring Boot, Mysql, MariaDB,Postgresql,Redis,Jenkins,Docker. Strong engineering professional with a B.Tech focused in Computer Science from Deemed University.

Updated on June 04, 2022

Comments

  • Akash Sourav Nayak
    Akash Sourav Nayak almost 2 years

    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;
        };
    
  • Abhishek Kumar
    Abhishek Kumar over 5 years
    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.
  • Dylan Aspden
    Dylan Aspden over 5 years
    @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 github.com/sequelize/express-example/blob/master/models/… for an example.
  • Budi Mulyo
    Budi Mulyo about 5 years
    my question almost same, but little nested, jsfiddle.net/j74rt9y1/1 the second assosiation didn't works and no error,, anyone can helps ?
  • prit.patel
    prit.patel almost 5 years
    @DylanAspden How to create a login record for a user?
  • Mohd Samgan Khan
    Mohd Samgan Khan about 3 years
    here is the complete explaination how its done codebysamgan.com/…