简体   繁体   English

我如何在MySQL Nodejs中建立一对多关系?

[英]How can I have one-to-many relation in MySQL Nodejs?

I have 4 tables: Head, Teacher, Student, and Class. 我有4张桌子:校长,老师,学生和班级。 There is a many-to-many relation between Teacher, Student, and Class. 老师,学生和班级之间存在多对多关系。 Now I want to have a one-to-many relation from Head to Teacher, Student, and Class. 现在我想建立一个从头到老师,学生和班级的一对多关系。

I have written this code but it just adds HeadId to the Student table. 我已经编写了这段代码,但是只是将HeadId添加到Student表中。 How can I add HeadId to the other tables? 如何将HeadId添加到其他表?

This is the code in Head table: 这是Head表中的代码:

const bcrypt            = require('bcrypt');
const bcrypt_p          = require('bcrypt-promise');
const jwt               = require('jsonwebtoken');
const {TE, to}          = require('../../services/util.service');
const CONFIG            = require('../../config/config');

 /* jshint ignore:start*/
 module.exports = (sequelize, DataTypes) => {
 var Model = sequelize.define('Head', {
    first     : DataTypes.STRING,
    last      : DataTypes.STRING,
    email     : {type: DataTypes.STRING, allowNull: true, unique: true, 
 validate: { isEmail: {msg: "Email invalid."} }},
    phone     : {type: DataTypes.STRING, allowNull: true, unique: true, 
 validate: { len: {args: [7, 20], msg: "Phone number invalid, too short."}, 
 isNumeric: { msg: "not a valid phone number."} }},
    password  : DataTypes.STRING,
  });

 Model.associate = function(models){
    this.Heads = this.hasMany(models.Class, {as: 'ClassAdmins'});
 };
 Model.associate = function(models){
    this.Classes = this.belongsTo(models.Head);
 };

 Model.associate = function(models){
    this.Heads = this.hasMany(models.Teacher, {as: 'TeacherAdmins'});
 };
 Model.associate = function(models){
    this.Teachers = this.belongsTo(models.Head);
 };

 Model.associate = function(models){
    this.Heads = this.hasMany(models.Student, {as: 'StudentAdmins'});
 };
 Model.associate = function(models){
    this.Students = this.belongsTo(models.Head);
 };

 Model.beforeSave(async (user, options) => {
    let err;
    if (user.changed('password')){
        let salt, hash
        [err, salt] = await to(bcrypt.genSalt(10));
        if(err) TE(err.message, true);

        [err, hash] = await to(bcrypt.hash(user.password, salt));
        if(err) TE(err.message, true);

        user.password = hash;
    }
 });

 Model.prototype.comparePassword = async function (pw) {
    let err, pass
    if(!this.password) TE('password not set');

    [err, pass] = await to(bcrypt_p.compare(pw, this.password));
    if(err) TE(err);

    if(!pass) TE('invalid password');

    return this;
 }

 Model.prototype.getJWT = function () {
    let expiration_time = parseInt(CONFIG.jwt_expiration);
    return "Bearer "+jwt.sign({user_id:this.id}, CONFIG.jwt_encryption, 
 {expiresIn: expiration_time});
 };

 Model.prototype.toWeb = function (pw) {
    let json = this.toJSON();
    return json;
 };

  return Model;
 };
 /*jshint ignore:end*/

Help is appreciated 感谢帮助

I think I could solve it with this change: 我想我可以通过此更改解决它:

put all hasMany in one Model.associate and without any blongsTo 将所有hasMany放在一个Model.associate中并且没有任何blongsTo

My solution: 我的解决方案:

Model.associate = function(models){
    this.Heads = this.hasMany(models.Class, {as: 'ClassAdmins'});
    this.Heads = this.hasMany(models.Teacher, {as: 'TeacherAdmins'});
    this.Heads = this.hasMany(models.Student, {as: 'StudentAdmins'});
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM