Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associating 3 tables in SEQUELIZE

Tags:

sequelize.js

I am trying to associate 3 tables in sequelize.

The models I have created are as follows.

enter image description here

Users model

var Sequelize = require('sequelize');

module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define('User', {
    uuid: {
      type: Sequelize.STRING,
      primaryKey: true
    },
    first_name: {
      type: Sequelize.STRING,
      allowNull: false
    },
    last_name: {
      type: Sequelize.STRING,
      allowNull: false
    },
    birth_date: {
      type: Sequelize.DATE,
      allowNull: false
    },
    gender: {
      type: Sequelize.STRING,
      allowNull: true
    },
    email_id: {
      type: Sequelize.STRING,
      allowNull: false,
      validate: {
        isEmail: true
      }
    },
    contact_number: {
      type: Sequelize.STRING,
      allowNull: false,
      validate: {
        isNumeric: true
      }
    }
  }, {
    classMethods: {
      associate: function(models) {
        User.hasMany(models.UserSchool)
      }
    }
  });

  return User;
};

Schools model

var Sequelize = require('sequelize');

module.exports = function(sequelize, DataTypes) {
  var School = sequelize.define('School', {
    school_id: {
      type: Sequelize.STRING,
      primaryKey: true
    },
    name: {
      type: Sequelize.STRING,
      allowNull: false
    },
    address: {
      type: Sequelize.TEXT,
      allowNull: false
    },
    city: {
      type: Sequelize.STRING,
      allowNull: false
    }
  }, {
    classMethods: {
      associate: function(models) {
        School.hasMany(models.UserSchool)
      }
    }
  });

  return School;
};

UserSchools model

var Sequelize = require('sequelize');

module.exports = function(sequelize, DataTypes) {
  var UserSchool = sequelize.define('UserSchool', {
    year_of_joining: {
      type: Sequelize.DATE,
      allowNull: true
    },
    year_of_passing: {
      type: Sequelize.DATE,
      allowNull: true
    },
    school_type: {
      type: Sequelize.STRING,
      allowNull: false
    },
    course: {
      type: Sequelize.STRING,
      allowNull: true
    }
  }, {
    classMethods: {
      associate: function(models) {
        UserSchool.belongsTo(models.User, {
          onDelete: "CASCADE",
          foreignKey: {
            allowNull: true
          }
        }),
        UserSchool.belongsTo(models.School, {
          onDelete: "CASCADE",
          foreignKey: {
            allowNull: true
          }
        });
      }
    }
  });

  return UserSchool;
};

When I retrieve users the userschools object is associated but school object is not to userschools. How can I create a 3 way association to retrieve the complete data?

Thanks in advance.

like image 634
Pradeep Rajashekar Avatar asked Apr 04 '16 04:04

Pradeep Rajashekar


People also ask

How do you do associations in Sequelize?

Creating associations in sequelize is done by calling one of the belongsTo / hasOne / hasMany / belongsToMany functions on a model (the source), and providing another model as the first argument to the function (the target). hasOne - adds a foreign key to the target and singular association mixins to the source.

How do you join tables in Sequelize?

There are two ways you can create JOIN queries and fetch data from multiple tables with Sequelize: Create raw SQL query using sequelize. query() method. Associate related Sequelize models and add the include option in your Sequelize query method.

How do you implement Many-To-Many relationship in Sequelize?

exports = db; belongsToMany() provides simple way to define the Sequelize Many-to-Many relationship. By passing "tutorial_tag" to through above, Sequelize will automatically generate a model named tutorial_tag as the through table (junction table), with two columns: tag_id and tutorial_id.


1 Answers

Can you try this, also?

user.js

classMethods: {
  associate: function(models) {
    // associations can be defined here
    User.belongsToMany(models.School, {
        through: {
            model: models.UserSchool
        },
        foreignKey: 'uuid'
    });
  }
}

school.js

classMethods: {
  associate: function(models) {
    //associations can be defined here
    School.belongsToMany(models.User, {
        through: {
            model: models.UserSchool
        },
        foreignKey: 'school_id'
    });
  }
}

No association in UserSchool.

like image 151
esmrkbr Avatar answered Oct 26 '22 02:10

esmrkbr