在 Sequelize 中的关联之间切换(同时一对多和多对多)
Switch between associatons in Sequelize (One To Many and Many to Many at the same time)
我有 table 个 User
、Assignment
、FavoriteAssignment
。 User
可以创建多个Assignment
,只会分配给他,即one to many
关联。
但是许多 User
可以将许多 Assignment
添加到他们的收藏夹中,即 many to many
通过 FavoriteAssignment
table.
关联
如何添加 User
创建的作业或他最喜欢的作业?我的意思是在使用 sequelize?
进行包含时,我应该如何在关联子之间切换
用户的关联:
class User extends Model {
static associate(models) {
User.hasMany(models.Assignment, { foreignKey: 'authorTelegramId'});
User.belongsToMany(models.Assignment, { through: 'FavoriteAssignments', foreignKey: 'telegramId'});
}
};
作业关联:
class Assignment extends Model {
static associate(models) {
Assignment.belongsTo(models.User, {foreignKey: 'authorTelegramId'});
Assignment.belongsToMany(models.User, {through: 'FavoriteAssignments', foreignKey: 'assignmentId'});
}
};
FavoriteAssignment 的关联:
class FavoriteAssignment extends Model {
static associate(models) {
FavoriteAssignment.belongsTo(models.User, {foreignKey: 'telegramId'});
FavoriteAssignment.belongsTo(models.Assignment, {foreignKey: 'assignmentId'});
}
};
我来到这个问题,因为当我做的时候:
const result = await db.User.findAll({
include: [{
model: db.Assignment
}]
});
它获取所有 User
最喜欢的作业,因为 belongsToMany
是最后定义的!
如果我评论 User.belongsToMany(models.Assignment, { through: 'FavoriteAssignments', foreignKey: 'telegramId'});
行,那么它将获取所有 User
创建的作业(one to many
关联)。
如果我有多个协会,我如何选择我想使用哪个协会?
基本上我必须这样命名关联:
User.hasMany(models.Assignment, { foreignKey: 'authorTelegramId', as: 'assignments'});
User.belongsToMany(models.Assignment, { through: 'FavoriteAssignments', foreignKey: 'telegramId', as: 'favoriteAssignments'});
然后调用它:
const result = await db.User.findAll({
include: ['assignments']
});
const result1 = await db.User.findAll({
include: ['favoriteAssignments']
});
我有 table 个 User
、Assignment
、FavoriteAssignment
。 User
可以创建多个Assignment
,只会分配给他,即one to many
关联。
但是许多 User
可以将许多 Assignment
添加到他们的收藏夹中,即 many to many
通过 FavoriteAssignment
table.
如何添加 User
创建的作业或他最喜欢的作业?我的意思是在使用 sequelize?
用户的关联:
class User extends Model {
static associate(models) {
User.hasMany(models.Assignment, { foreignKey: 'authorTelegramId'});
User.belongsToMany(models.Assignment, { through: 'FavoriteAssignments', foreignKey: 'telegramId'});
}
};
作业关联:
class Assignment extends Model {
static associate(models) {
Assignment.belongsTo(models.User, {foreignKey: 'authorTelegramId'});
Assignment.belongsToMany(models.User, {through: 'FavoriteAssignments', foreignKey: 'assignmentId'});
}
};
FavoriteAssignment 的关联:
class FavoriteAssignment extends Model {
static associate(models) {
FavoriteAssignment.belongsTo(models.User, {foreignKey: 'telegramId'});
FavoriteAssignment.belongsTo(models.Assignment, {foreignKey: 'assignmentId'});
}
};
我来到这个问题,因为当我做的时候:
const result = await db.User.findAll({
include: [{
model: db.Assignment
}]
});
它获取所有 User
最喜欢的作业,因为 belongsToMany
是最后定义的!
如果我评论 User.belongsToMany(models.Assignment, { through: 'FavoriteAssignments', foreignKey: 'telegramId'});
行,那么它将获取所有 User
创建的作业(one to many
关联)。
如果我有多个协会,我如何选择我想使用哪个协会?
基本上我必须这样命名关联:
User.hasMany(models.Assignment, { foreignKey: 'authorTelegramId', as: 'assignments'});
User.belongsToMany(models.Assignment, { through: 'FavoriteAssignments', foreignKey: 'telegramId', as: 'favoriteAssignments'});
然后调用它:
const result = await db.User.findAll({
include: ['assignments']
});
const result1 = await db.User.findAll({
include: ['favoriteAssignments']
});