无法使用 belongsToMany 正确使用多对多
Unable to correctly use many-to-many using belongsToMany
一段时间以来,我一直在努力解决这个问题。我读到 hasMany
在尝试建立 many-to-many
关联时不是正确的方法。我有两个表来创建以下关联 User
和 Follower
(现在这个名字很糟糕)。
追随者
const follower = (sequelize, DataTypes) => {
const Follower = sequelize.define('follower', {
follower_id: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
notNull: true
}
},
following_id: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
notNull: true
}
}
});
return Follower;
};
module.exports = follower;
接下来我们将用户模型设为
const user = (sequelize, DataTypes) => {
const User = sequelize.define('user', {
email: {
type: DataTypes.STRING,
unique: true,
allowNull: false,
validate: {
isEmail: true,
notEmpty: true
}
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: true
}
},
username: {
type: DataTypes.STRING,
unique: true,
allowNull: false
}
});
User.associate = (models) => {
User.hasMany(models.post, { foreignKey: 'user_id' });
User.belongsToMany(models.user, {
through: 'users_followers',
foreignKey: 'follower_id',
otherKey: 'following_id',
as: 'followers',
});
User.belongsToMany(models.user, {
through: 'users_followers',
foreignKey: 'following_id',
otherKey: 'follower_id',
as: 'following',
});
return User;
};
module.exports = user;
关于创建多对多关联,我做错了什么吗?我问是因为我一直在尝试差异变化但是在尝试 include: [db.follower]
时无法执行查询
感谢大家的帮助
附加信息
查询 运行 是:
const fetchUser = async (root, args, context) => {
const { db, user } = await context();
const { id } = args;
const fetchedUser = await db.user.findOne({
attributes: {
include: [
[sequelize.literal('(select count(*) from users_followers where users_followers.follower_id=users.id)'), 'followers_count'],
]
},
include: [
'followers',
'following'
],
where: {
id
},
raw: true
});
return fetchedUser;
在 include
选项中,您始终应指明位于已定义关联另一端的模型。在这种情况下,它是具有两个不同别名的 User
模型:
include: [
{
model: db.user,
as: 'followers'
}, {
model: db.user,
as: 'following'
}],
一段时间以来,我一直在努力解决这个问题。我读到 hasMany
在尝试建立 many-to-many
关联时不是正确的方法。我有两个表来创建以下关联 User
和 Follower
(现在这个名字很糟糕)。
追随者
const follower = (sequelize, DataTypes) => {
const Follower = sequelize.define('follower', {
follower_id: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
notNull: true
}
},
following_id: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
notNull: true
}
}
});
return Follower;
};
module.exports = follower;
接下来我们将用户模型设为
const user = (sequelize, DataTypes) => {
const User = sequelize.define('user', {
email: {
type: DataTypes.STRING,
unique: true,
allowNull: false,
validate: {
isEmail: true,
notEmpty: true
}
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: true
}
},
username: {
type: DataTypes.STRING,
unique: true,
allowNull: false
}
});
User.associate = (models) => {
User.hasMany(models.post, { foreignKey: 'user_id' });
User.belongsToMany(models.user, {
through: 'users_followers',
foreignKey: 'follower_id',
otherKey: 'following_id',
as: 'followers',
});
User.belongsToMany(models.user, {
through: 'users_followers',
foreignKey: 'following_id',
otherKey: 'follower_id',
as: 'following',
});
return User;
};
module.exports = user;
关于创建多对多关联,我做错了什么吗?我问是因为我一直在尝试差异变化但是在尝试 include: [db.follower]
感谢大家的帮助
附加信息
查询 运行 是:
const fetchUser = async (root, args, context) => {
const { db, user } = await context();
const { id } = args;
const fetchedUser = await db.user.findOne({
attributes: {
include: [
[sequelize.literal('(select count(*) from users_followers where users_followers.follower_id=users.id)'), 'followers_count'],
]
},
include: [
'followers',
'following'
],
where: {
id
},
raw: true
});
return fetchedUser;
在 include
选项中,您始终应指明位于已定义关联另一端的模型。在这种情况下,它是具有两个不同别名的 User
模型:
include: [
{
model: db.user,
as: 'followers'
}, {
model: db.user,
as: 'following'
}],