Sequelize.js 在SEQUELIZE中关联3个表

Sequelize.js 在SEQUELIZE中关联3个表,sequelize.js,Sequelize.js,我试图在sequelize中关联3个表 我创建的模型如下 用户模型 var Sequelize = require('sequelize'); module.exports = function(sequelize, DataTypes) { var User = sequelize.define('User', { uuid: { type: Sequelize.STRING, primaryKey: true }, first_name

我试图在sequelize中关联3个表

我创建的模型如下

用户模型

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;
};
学校模式

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;
};
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;
};
用户学校模型

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;
};
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;
};
当我检索用户时,userschools对象是关联的,但school对象不是userschools。如何创建三方关联以检索完整数据


提前谢谢。

你也可以试试这个吗

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'
    });
  }
}
学校里没有关联

User.belongsToMany(models.School, {
    through: {
        model: models.UserSchool
    },
    foreignKey: 'uuid'
});

School.belongsToMany(models.User, {
    through: {
        model: models.UserSchool
    },
    foreignKey: 'school_id'
});

这应该能解决你的问题

让我知道是否有更好的方法在MySQL中保存此类数据。合并Schools和UserSchools表不是一个选项,因为我需要维护一个具有唯一School Id:)的表。非常感谢:)他们对Sequelize文档的看法是正确的(虽然不是很好,但Sequelize本身就很棒):)嘿,你能帮我关联4个表吗?你能帮我关联4个表吗?会很有帮助的谢谢