在 Sequelize 中填充没有关联 ID 的数据

Populate data without association id in Sequelize

我有 4 tables,用户,个人资料,专业,user_specialities。我正在使用续集。我的用户模型是-->

const User = sequelize.define('user', {
user_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true,
    },
    IsDeleted: {
        type:Sequelize.BOOLEAN,
    },
    status: {
        type:Sequelize.BOOLEAN,
},
});

我的个人资料模型-->

const Profile= sequelize.define('profile', {
    profile_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true,
    },
    first_name: {
        type:Sequelize.STRING,
    },
    last_name: {
        type:Sequelize.STRING,
    },
},
{
    indexes: [
        {
            unique: true,
            fields: ['user_id']
        }
    ]
});   
Chef_Info.belongsTo(User, {foreignKey: 'user_id'});

专业table-->

    const Profile= sequelize.define('speciality', {
        speciality_id: {
            type: Sequelize.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
        },
        title: {
            type:Sequelize.STRING,
        },
}

user_specialities 持有用户和专业之间的多对多关系。我已经使用了 belongsToMany 这样的关联 -

Speciality.belongsToMany(User, {through: 'user_speciality',foreignKey: 'speciality_id', otherKey: 'user_id'});
User.belongsToMany(Speciality, {through: 'user_speciality',foreignKey: 'user_id', otherKey: 'speciality_id'});

现在,我想在我有 user_id、first_name、last_name 的地方获取数据,还有专业。例如-->

first_name  | last_name     | specialities
Khabib      | Nurmagamedov  | Athlete, Wrestler
Tony        | Ferguson      | Athlete, Striker

我在 MySql 中尝试过,查询应该是 -->

SELECT profiles.first_name,profiles.last_name,GROUP_CONCAT(specialities.title)
FROM (
users
    LEFT JOIN profiles
         ON users.user_id = profiles.user_id 
    LEFT JOIN user_specialities
        ON users.user_id = user_specialities.user_id
    LEFT JOIN specialities
        ON chef_specialities.speciality_id = specialities.speciality_id
)
WHERE(
    AND users.IsDeleted = false
    And users.status = true
)
GROUP BY users.user_id;

但我正在努力将其转换为续集。我已经走到这一步-->

UserSpecialities.findAll({
    attributes:[sequelize.fn('GROUP_CONCAT', sequelize.col('title')), 'speciality_id'],
    include: [
    { 
       model: User,
       attributes: [],
       where:{
           IsRemoved: false,
           status: true,
       },
    },
    { model: Speciality,
         attributes: [],
    },
    ],
    group:['user_id']

它提供了用户的专业-->

[
    {
        "speciality_id": "Athlete, Wrestler",
    },
    {
        "speciality_id": "Athlete, Striker",
    }
]

但我未能填充配置文件数据 here.If 我尝试包含配置文件,它没有显示任何关联错误,或者我尝试在用户模型中添加 hasMany 它再次抛出错误。遇到这种情况怎么办? 提前致谢。

因为 Profile 与 Users 而不是 UserSpecialties 相关联,所以使用两个查询来完成此操作更有意义。

像上面那样获取您的用户专长,然后获取这些用户 ID 并获取其关联的配置文件(应该类似于 User.getProfile,具体取决于您的关联设置方式)。

现在您的 UserSpecialties 属性仅设置为 return specialty_id 和标题,如果您将 userId 添加到这些属性,那么您可以使用这些 userId 来查询您的个人资料 table。一个 then promise 对这样的事情很有效,比如:

UserSpecialties.findAll({...}).then((userSpecialties) => {
  User.getProfile({where: {userId: userSpecialties.userId}, attributes: {'first_name', 'last_name'})
});

根据您具体尝试对信息执行的操作,您可能需要对其进行一些调整,但一般的想法是将 userId 添加为属性,然后向您的用户 table 查询配置文件使用 UserSpecialties 查询结果中的 userId 应该可以很好地处理您想要的数据。

尽管查看您的模型,将个人资料和用户合二为一可能有意义 table,其中用户具有 first_name、last_name、用户 ID 和状态。由于 Profile 没有很多其他内容,如果您需要 userProfile 的特定内容,您可以在用户模型上使用范围。