Sequelize包括范围为1的对象:m constraint = false

问题描述 投票:2回答:1

我想在其他表中使用相同的表Photo。我读了Sequelize doc here,他们使用范围并设置约束= 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'
});

我的问题是:我如何查询包含每个帖子的列表评论的帖子列表(如果可以的话,有限制)。谢谢。

javascript node.js orm sequelize.js
1个回答
1
投票

我如何查询包含每个帖子的列表评论的帖子列表

在这里,试试这个

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

© www.soinside.com 2019 - 2024. All rights reserved.