【发布时间】:2016-03-11 04:34:29
【问题描述】:
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});
在“标签”页面上,我想获取标签数据、相关帖子并通过分页显示它们。所以我应该限制帖子。但是如果我尝试将它们限制在“包含”中
Tag.findOne({
where: {url: req.params.url},
include: [{
model : Post,
limit: 10
}]
}).then(function(tag) {
//handling results
});
我收到以下错误:
Unhandled rejection Error: Only HasMany associations support include.separate
如果我尝试切换到“HasMany”关联,我会收到以下错误
Error: N:M associations are not supported with hasMany. Use belongsToMany instead
从其他方面的文档中说,限制选项“仅支持 include.separate=true”。如何解决这个问题?
【问题讨论】:
标签: javascript node.js sequelize.js