维持多对多自我关联

时间:2019-03-17 15:34:07

标签: javascript orm sequelize.js

我正在尝试创建一个模型Users,该模型与其自身具有多对多关联,以允许用户关注另一个用户。在一个查询中,我要检索Users后跟当前用户;在另一个查询中,我想检索跟随当前用户的人员。

这是我的Users模型:

module.exports = (sequelize, Sequelize) => {
    const Users = sequelize.define(
        'Users',
        {
            id: {
                type: Sequelize.INTEGER,
                autoIncrement: true,
                primaryKey: true,
            },

            name: {
                type: Sequelize.STRING,
            },
        },
    );
    Users.associate = function(models) {
        Users.belongsToMany(Users, { as: 'following', through: models.UsersUsers });
    };

    return Users;
};

我声明UsersUsers,以防万一我需要在其中添加任何字段:

module.exports = (sequelize, Sequelize) => {
    const UsersUsers = sequelize.define(
        'UsersUsers',
        {}
    );
    UsersUsers.associate = function(models) {};

    return UsersUsers;
};

然后我以以下方式查询Users

models.Users.findOne({
    where: {
        id: req.params.id,
    },
    include: [
        {
            model: models.Users,
            as: 'following',
        },
    ],
})
    .then((results) => {
        return res.send({
            User: results,
        });
    })
    .catch((error) => {
        return res.send(String(error));
    });

我得到这个结果:

{
    "User": {
        "id": 1,
        "name": "User1",
        "following": [
            {
                "id": 2,
                "name": "User2",
                "UsersUsers": {
                    "UserId": 1,
                    "followingId": 2
                }
            },
            {
                "id": 3,
                "name": "User3",
                "UsersUsers": {
                    "UserId": 1,
                    "followingId": 3
                }
            },
            {
                "id": 4,
                "name": "User4",
                "UsersUsers": {
                    "UserId": 1,
                    "followingId": 4
                }
            }
        ]
    }
}

现在的问题:

  1. 在当前查询中,如何从结果中排除“ UsersUsers”? attributes: { exclude: ['UsersUsers'] }无效...

  2. 如何创建查询来检索当前用户,而不是跟随他的用户?

谢谢!

- 编辑:

问题1的解决方案是将through: { attributes: [] }添加到所包含的模型中:

models.Users.findOne({
    where: {
        id: req.params.id,
    },
    include: [
        {
            model: models.Users,
            as: 'following',
            through: {
                attributes: [],
            },
        },
    ],
})
    .then((results) => {
        return res.send({
            User: results,
        });
    })
    .catch((error) => {
        return res.send(String(error));
    });

问题2仍未解决!

1 个答案:

答案 0 :(得分:0)

Users.findAll({
        include: [
    {
        model: models.Users,
        as: 'following',
        through: {
            attributes: [],
        },
    },
],
    where : {
        id : [connection.literal(` write raw sql query to get followingId here`)]
    }
})
.then(result => {
    res.json(result);
}).catch(error=>{
    res.json(error);
});

我不确定这是否行得通,还是继续尝试一下,并让我知道这是否行得通,或者您是否找到了解决方案。