Sequelize:多个 where 子句 [英] Sequelize: multiple where clause

查看:52
本文介绍了Sequelize:多个 where 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下表格:

文章 - 用户 - 标签 - 关注者 - 订阅

Article - User - Tag - Followers - Suscribes

文章属于用户(fk:文章表中的 userId)

Article belongs to User (fk: userId in Article table)

文章可以有很多标签.这是生成的 tagarticle 表:

Article can have many tag. Here is the generated tagarticle table:

这是关注者表:

和 Suscribes 表:

And the Suscribes table:

一个用户可以关注多个用户并订阅一个国家(payId)、一个标签或一篇文章(用于通知).

A user can follow many users and suscribe to a country(payId), a tag or an article(for notifications).

如何查询特定用户的所有关注用户和订阅国家或标签的文章?

How to query all articles of followed users and suscribed country or tag for a specific user?

推荐答案

我假设您询问 Sequelize 执行查询的方式.我不确定我是否正确理解你的问题.您正在寻找两个查询:

I assume that you ask about Sequelize way of doing the query. I am not sure if I understand your question correctly. You are looking for two queries:

  • 查询关注用户的所有文章,
  • 查询特定用户订阅的国家/地区/标签/文章,

让我从模型之间的关联开始.

Let me start with the associations made between the models.

// in User model definition
User.belongsToMany(User, { as: 'Followers', through: 'Followers', foreignKey: 'userId', otherKey: 'followId' });
User.hasMany(Subscribe, { foreignKey: 'userId' });
User.hasMany(Article, { foreignKey: 'userId' });

使用上面的关联,我们现在可以查询关注用户的所有文章

With use of above association we are now able to query all articles of followed users

models.User.findByPrimary(1, {
    include: [
        {
            model: models.User,
            as: 'Followers',
            include: [ models.Article ]
        }
    ]
}).then(function(user){
    // here you have user with his followers and their articles
});

上面的查询会产生类似的结果

Above query would generate result similar to

{
    id: 1,
    Followers: [
        {
            id: 4,
            Articles: [
                {
                    id: 1,
                    title: 'article title' // some example field of Article model
                }
            ]
        }
    ]
}

如果你想查询特定用户订阅的国家/标签/文章,你必须在订阅模型中做另一个关联

If you want to query country/tag/article subscribed by specific user, you would have to make another associations in Subscribe model

// in Subscribe model definition
Subscribe.belongsTo(Tag, { foreignKey: 'tagId' });
Subscribe.belongsTo(Article, { foreignKey: 'articleId' });
Subscribe.belongsTo(Country, { foreignKey: 'payId' });

现在我们拥有执行您要求的第二个查询所需的所有关联

Now we have all the associations required to perform the second query you asked for

models.User.findByPrimary(1, {
    include: [
        {
            model: models.Subscribe,
            include: [ models.Tag, models.Country, models.Article ]
        }
    ]
}).then(function(user){
    // here you get user with his subscriptions
});

在这个例子中,你通过 user.Subscribes 获取用户的所有订阅,这将具有嵌套的属性 TagCountry 和 <代码>文章.如果用户订阅了 Tag,则 CountryArticle 在这种情况下都将是 NULL.

In this example you get user with all his subscriptions accessed via user.Subscribes, which will have nested attributes Tag, Country and Article. If user subscribed to Tag, both Country and Article would be NULL in this case.

这篇关于Sequelize:多个 where 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆