我该如何在续集中进行此查询
(SELECT rol FROM roles INNER JOIN user_roles ON roles.id = user_roles.roleId)
这是我的模式
sequelize.define('user_roles', {
userId: Sequelize.INTEGER,
roleId: Sequelize.INTEGER
});
和角色架构
sequelize.define('roles', {
rol: Sequelize.STRING
});
和用户角色架构
sequelize.define('user_roles', {
userId: Sequelize.INTEGER,
roleId: Sequelize.INTEGER
});
在用户角色中,我添加用户ID和角色ID以进行内部联接
答案 0 :(得分:1)
const UserRole = sequelize.define('UserRole', {
});
const User = sequelize.define('User', {
name: {
type: DataTypes.STRING,
allowNull: false,
required: true
},
});
const Role = sequelize.define("Roles", {
name: {
type: DataTypes.STRING,
allowNull: false
}
})
Role.belongsToMany(User, { through: UserRole })
User.belongsToMany(Role, { through: UserRole })
然后执行查询
const roles = await Role.findAll({
include: [
{
model: User,
through: UserRole,
required: true
}
]
})
可以在github sscce上找到完整的运行示例。您可以克隆并运行整个演示。可以找到主要的源文件here