在模型创建查询期间使关联化?

时间:2018-11-21 12:49:15

标签: mysql sequelize.js mysql2

这是我的示例数据库结构。这是我第一次尝试续集ORM。我只是想知道我正确完成了关联。如果我做错了方式,请添加您的建议

我的模型是:

    **Organization**

 const Organization = sequelize.define('Organization', {
    org_name: DataTypes.STRING

  }, {});
  Organization.associate = function(models) {
    Organization.hasMany(models.User,{ through: 'User_Roles'});
    Organization.hasMany(models.Department ,{ through: 'User_Roles'});
  };


    **Department**



  const Department = sequelize.define('Department', {
    name: DataTypes.STRING

  }, {});
  Department.associate = function(models) {
    Department.belongsTo(models.Organization);

  };

     **user**
  const User = sequelize.define('User', {

    username: DataTypes.STRING,

  }, {});
  User.associate = function(models) {
    User.belongsToMany(models.Organization,{ through: 'User_Roles'});
    User.belongsToMany(models.Department,{ through: 'User_Roles'});
    User.belongsToMany(models.Role,{ through: 'User_Roles'});
  };

    **role**
  const Role = sequelize.define('Role', {
    role_name: DataTypes.STRING
  }, {});
  Role.associate = function(models) {
    Role.belongsToMany(models.User,{ through: 'User_Roles'});
  };

enter image description here

0 个答案:

没有答案