2016-04-04 10 views
6

3つのテーブルを続行して関連付けようとしています。SEQUELIZEで3つのテーブルを関連付ける

私が作成したモデルは以下の通りです。

enter image description here

ユーザーモデル

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

UserSchoolsモデル

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オブジェクトがあります関連しているが学校のオブジェクトはユーザーの学校にはありません。完全なデータを取得するために3ウェイ関連を作成するにはどうすればよいですか?

ありがとうございます。

+0

MySQLでこの種のデータを保存するより良い方法があるかどうかを教えてください。ユニークなスクールIDのテーブルを維持する必要があるので、スクールとUserSchoolsのマージテーブルはオプションではありません:) –

答えて

3

これも試すことができますか?

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

UserSchoolではありません関連。

+0

チャームのように働いていました。ありがとう:)本当に彼らがSequelizeのドキュメントについて言うことは本当ですか?それはそれほど素晴らしいものではありませんが、Sequelizeはそれだけで素晴らしいです:) –

+0

ちょっと、あなたは4つのテーブルを関連付けることを手伝ってくれますか? http://stackoverflow.com/questions/41671377/sequelize-associating-4-tables –

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

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

これは問題を解決するはずです。

+0

4つのテーブルを関連付けることができますか?非常に役立つだろうhttp://stackoverflow.com/questions/41671377/sequelize-associating-4-tablesありがとう –

関連する問題