继承多个 HasMany 问题
Sequelize multiple HasMany issue
我的架构
db.user.hasMany(db.appointment, {
foreignKey: 'user_id'
})
db.user.hasMany(db.appointment, {
foreignKey: 'doctor_id'
})
db.appointment.belongsTo(db.user, {
foreignKey: 'user_id'
})
db.appointment.belongsTo(db.user, {
foreignKey: 'doctor_id'
})
table用户是医生和用户的共同点
之后我创建了 2 个约会
await Appointment.create({user_id: 13, doctor_id: 10 });
await Appointment.create({user_id: 14, doctor_id: 10 });
并尝试查询 ID 为 13 的用户的约会
let user = await User.findByPk(13);
return res.json(user.getAppointments())
我预计只有一次预约
{
"id": 1,
"createdAt": "2022-04-11T09:42:45.000Z",
"updatedAt": "2022-04-11T09:42:45.000Z",
"user_id": 13,
"doctor_id": 10
}
但实际上我得到了
[
{
"id": 1,
"createdAt": "2022-04-11T09:42:45.000Z",
"updatedAt": "2022-04-11T09:42:45.000Z",
"user_id": 13,
"doctor_id": 10
},
{
"id": 2,
"createdAt": "2022-04-11T09:42:55.000Z",
"updatedAt": "2022-04-11T09:42:55.000Z",
"user_id": 14,
"doctor_id": 10
}
]
正如你看到的方法user.getAppointments() return me 2 appointment for user 13, 但实际上他只有一个约会(另一个约会属于用户14)。我想我在这里错了或遗漏了一些东西(比如 Java Hibernate 中的 mappedBy)
db.user.hasMany(db.appointment, {
foreignKey: 'user_id'
})
db.user.hasMany(db.appointment, {
foreignKey: 'doctor_id'
})
db.appointment.belongsTo(db.user, {
foreignKey: 'user_id'
})
db.appointment.belongsTo(db.user, {
foreignKey: 'doctor_id'
})
能帮忙改一下吗?提前致谢
如果您定义了从同一个模型到另一个相同模型的多个关联,那么您应该为关联模型指定唯一的别名,以便在所有查询中区分它们:
db.user.hasMany(db.appointment, {
foreignKey: 'user_id',
as: 'userAppointments'
})
db.user.hasMany(db.appointment, {
foreignKey: 'doctor_id',
as: 'doctorAppointments'
})
db.appointment.belongsTo(db.user, {
foreignKey: 'user_id',
as: 'user'
})
db.appointment.belongsTo(db.user, {
foreignKey: 'doctor_id',
as: 'doctor'
})
获取用户约会:
user.getUserAppointments()
预约医生:
doctor.getDoctorAppointments()
预约医生:
appointment.getDoctor()
获取约会的用户:
appointment.getUser()
我的架构
db.user.hasMany(db.appointment, {
foreignKey: 'user_id'
})
db.user.hasMany(db.appointment, {
foreignKey: 'doctor_id'
})
db.appointment.belongsTo(db.user, {
foreignKey: 'user_id'
})
db.appointment.belongsTo(db.user, {
foreignKey: 'doctor_id'
})
table用户是医生和用户的共同点
之后我创建了 2 个约会
await Appointment.create({user_id: 13, doctor_id: 10 });
await Appointment.create({user_id: 14, doctor_id: 10 });
并尝试查询 ID 为 13 的用户的约会
let user = await User.findByPk(13);
return res.json(user.getAppointments())
我预计只有一次预约
{
"id": 1,
"createdAt": "2022-04-11T09:42:45.000Z",
"updatedAt": "2022-04-11T09:42:45.000Z",
"user_id": 13,
"doctor_id": 10
}
但实际上我得到了
[
{
"id": 1,
"createdAt": "2022-04-11T09:42:45.000Z",
"updatedAt": "2022-04-11T09:42:45.000Z",
"user_id": 13,
"doctor_id": 10
},
{
"id": 2,
"createdAt": "2022-04-11T09:42:55.000Z",
"updatedAt": "2022-04-11T09:42:55.000Z",
"user_id": 14,
"doctor_id": 10
}
]
正如你看到的方法user.getAppointments() return me 2 appointment for user 13, 但实际上他只有一个约会(另一个约会属于用户14)。我想我在这里错了或遗漏了一些东西(比如 Java Hibernate 中的 mappedBy)
db.user.hasMany(db.appointment, {
foreignKey: 'user_id'
})
db.user.hasMany(db.appointment, {
foreignKey: 'doctor_id'
})
db.appointment.belongsTo(db.user, {
foreignKey: 'user_id'
})
db.appointment.belongsTo(db.user, {
foreignKey: 'doctor_id'
})
能帮忙改一下吗?提前致谢
如果您定义了从同一个模型到另一个相同模型的多个关联,那么您应该为关联模型指定唯一的别名,以便在所有查询中区分它们:
db.user.hasMany(db.appointment, {
foreignKey: 'user_id',
as: 'userAppointments'
})
db.user.hasMany(db.appointment, {
foreignKey: 'doctor_id',
as: 'doctorAppointments'
})
db.appointment.belongsTo(db.user, {
foreignKey: 'user_id',
as: 'user'
})
db.appointment.belongsTo(db.user, {
foreignKey: 'doctor_id',
as: 'doctor'
})
获取用户约会:
user.getUserAppointments()
预约医生:
doctor.getDoctorAppointments()
预约医生:
appointment.getDoctor()
获取约会的用户:
appointment.getUser()