[英]Sequelize Many to Many realtionship
I'm using Sequelize, and I have two models, with many to many relationships, the employees model it has RegistrationNmuber as Primary Key, firstName, LastName amongst other information, and a message model it has an id attribute, from attribute which is the RegistrationNumber of the employee, and the to attribute contain the another RegistrationNumber of another employee, and message attribute, what I want to do is find the information of employee from the 'from attribute of message model', is it possible to do it and how??我正在使用 Sequelize,我有两个模型,具有多对多关系,员工 model 它具有 RegistrationNmuber 作为主键、名字、姓氏以及其他信息,以及一条消息 model 它有一个 id 属性,来自属性是员工的RegistrationNumber,to属性包含另一个员工的RegistrationNumber和message属性,我想做的是从“消息模型的from属性”中找到员工的信息,是否可以这样做以及如何?? if not can anybody please suggest a solution
如果不能,请提出解决方案
// employee model:
module.exports = (sequelize,Sequelize) => {
const Employee = sequelize.define('Employee' ,{
RegistrationNumber :{
type: Sequelize.BIGINT,
primaryKey: true
},
CIN : {
type:Sequelize.BIGINT,
unique: true
},
FirstName: {
type: Sequelize.STRING,
},
LastName: {
type: Sequelize.STRING
},
image:{
type: Sequelize.BLOB
},
DateofBirth: {
type: Sequelize.DATE,
},
PlaceOfBirth: {
type: Sequelize.STRING,
},
Address: {
type: Sequelize.TEXT,
},
Email: {
type: Sequelize.TEXT,
unique: true
},
PhoneNumber: {
type: Sequelize.BIGINT,
},
DateofNamingRank :{
type: Sequelize.DATE
},
DateofDesignationAdministration :{
type: Sequelize.DATE
},
Type :{
type: Sequelize.STRING
},
DateofDemarcation :{
type: Sequelize.DATE
},
Item :{
type: Sequelize.STRING
},
Rank :{
type: Sequelize.STRING
},
Case :{
type: Sequelize.STRING
},
Position :{
type: Sequelize.STRING
},
ActualWork :{
type:Sequelize.STRING
},
Wire :{
type: Sequelize.STRING
},
Plan :{
type: Sequelize.STRING
},
Specialization :{
type: Sequelize.STRING
},
Intrest :{
type: Sequelize.STRING
},
Role :{
type:Sequelize.STRING
},
Login:{
type:Sequelize.STRING
},
Password :{
type:Sequelize.STRING
}
},
); return Employee;
}
// message Model
module.exports =( sequelize,Sequelize)=>{
const Messages = sequelize.define('Messages' ,{
id :{
type: Sequelize.BIGINT,
primaryKey: true,
autoIncrement: true
},
from :{
type : Sequelize.BIGINT
},
to :{
type : Sequelize.BIGINT
} ,
message:{
type: Sequelize.TEXT
}
})
return Messages;
} }
// Relation
db.employee.belongsToMany(db.messages,{
through : "employee_Messages",
} )
db.messages.belongsToMany(db.employee ,
{through:"employee_Messages"})
db.messages.belongsTo(db.employee, { foreignKey: "from" }) db.messages.belongsTo(db.employee, { foreignKey: "to" }) messages.find({ where: { id: messageId }, include: [{ model: employee, as: from, required: true }, { model: employee, as: to, required: true } ] }).then(message => { const userfrom = message.from; const userto = message.to; })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.