且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-06 21:08:07

我知道这个问题很老了,但对于那些仍然遇到这个问题的人来说,有一个简单的解决方法.

I know this question is old but for those of you still experiencing this issue, there's a simple workaround.

由于 Sequelize 向实例添加了自定义方法在关联模型中,您可以将代码重构为如下所示:

Since Sequelize adds custom methods to instances of associated models, you could restructure your code to something like this:

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.