我想对其他表格使用相同的表格 Photo
。
我阅读了 Sequelize 文档 here
他们使用范围并像这样设置 constraint = false
const Comment = this.sequelize.define('comment', {
title: Sequelize.STRING,
commentable: Sequelize.STRING,
commentable_id: Sequelize.INTEGER
});
Comment.prototype.getItem = function(options) {
return this['get' + this.get('commentable').substr(0, 1).toUpperCase() + this.get('commentable').substr(1)](options);
};
Post.hasMany(this.Comment, {
foreignKey: 'commentable_id',
constraints: false,
scope: {
commentable: 'post'
}
});
Comment.belongsTo(this.Post, {
foreignKey: 'commentable_id',
constraints: false,
as: 'post'
});
我的问题是:我如何查询包含每个帖子的列表评论的帖子列表(如果可用则有限制)。 谢谢。
最佳答案
HOW I can query list of Post that include list comments for each post
来,试试这个
Post
.find({
where: { /* include where condition if you have or remove this */ },
include: [{
model: Comment,
as: 'comments',
}],
})
.then(function(result) {
// check result.dataValues. it should contain `comments` array
});
我想这个限制还不可用。检查此更新 https://github.com/sequelize/sequelize/issues/1897
关于javascript - Sequelize 包含范围为 1 :m constraint = false 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49080058/