简体   繁体   中英

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

I have 4 tables: Head, Teacher, Student, and Class. 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. How can I add HeadId to the other tables?

This is the code in Head table:

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

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'});
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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