简体   繁体   English

如何限制 Sequelize ORM 中的连接行(多对多关联)?

[英]How to limit joined rows (many-to-many association) in Sequelize ORM?

There are two models defined by Sequelize: Post and Tag with many-to-many association. Sequelize 定义了两种模型:具有多对多关联的 Post 和 Tag。

Post.belongsToMany(db.Tag, {through: 'post_tag', foreignKey: 'post_id', timestamps: false});
Tag.belongsToMany(db.Post, {through: 'post_tag', foreignKey: 'tag_id',timestamps: false});

On a "tag" page I want to get tag data, associated posts and show them with pagination.在“标签”页面上,我想获取标签数据、相关帖子并通过分页显示它们。 So I should limit posts.所以我应该限制帖子。 But If I try limit them inside "include"但是如果我尝试将它们限制在“包含”中

Tag.findOne({
    where: {url: req.params.url},
    include: [{
        model : Post,
        limit: 10
    }]
}).then(function(tag) {
    //handling results
});

I get following error:我收到以下错误:

Unhandled rejection Error: Only HasMany associations support include.separate

If I try to switch to "HasMany" associations I get following error如果我尝试切换到“HasMany”关联,我会收到以下错误

Error: N:M associations are not supported with hasMany. Use belongsToMany instead

And from other side documentation says that limit option "only supported with include.separate=true".从其他方面的文档中说,限制选项“仅支持 include.separate=true”。 How to solve this problem?如何解决这个问题呢?

Having the same exact problem here. 这里有同样的问题。 From what I gathered, to use limit/offset you need the include.separate, which isn't supported for belongsToMany associations yet, so what you're trying to do isn't supported at the time of this post. 从我收集的内容到使用限制/偏移量,你需要include.separate,这对于belongsToMany关联还不支持,所以在这篇文章发表时你不支持你想要做的事情。

There's already someone working on it, you can track it here . 已经有人在做,你可以在这里跟踪它。

I know this question is old but for those of you still experiencing this issue, there's a simple workaround. 我知道这个问题很老但是对于那些仍然遇到这个问题的人来说,有一个简单的解决方法。

Since Sequelize adds custom methods to instances of associated models, you could restructure your code to something like this: 由于Sequelize将自定义方法添加到关联模型的实例,因此您可以将代码重构为以下内容:

const tag = await Tag.findOne({ where: { url: req.params.url } });
const tagPosts = await tag.getPosts({ limit: 10 });

This would work the exact same way as your code but with limiting and offsetting possible. 这将与您的代码完全相同,但可以进行限制和偏移。

I know this is a very old question that has not been answered.我知道这是一个非常古老的问题,尚未得到解答。

To anyone still facing such an issue ,对于仍然面临此类问题的任何人

As the previous answer mentions that the belongsToMany associations do not support limit or offset..正如前面的答案所提到的, belongsToMany关联不支持限制或偏移。

Move the limit and offset outside your include array when dealing with Many to Many relations, although you are already querying as findOne(), passing limit or offset on the outer level will be activated on the inner relations that you have in your includes array ONLY in Many-Many Relations在处理多对多关系时,将限制和偏移移到包含数组之外,尽管您已经以 findOne() 的形式进行查询,但在外部级别传递限制或偏移量将仅在包含数组中的内部关系上被激活在多对多关系中

It will automatically handle the inner results limit and offset它将自动处理内部结果限制和偏移量

Tag.findOne({
    where: {url: req.params.url},
    include: [{
        model : Post,
        limit: 10 ❌ ( only exists in hasMany() )
    }]
    limit:10, ✅ ( This will solve your issue )

}).then(function(tag) {
    //handling results
});

I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM