javascript - Sequelize 包含范围为 1 :m constraint = false 的对象

标签 javascript node.js orm sequelize.js

我想对其他表格使用相同的表格 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/

相关文章:

javascript - 包含一个字符串且不包含另一字符串的行的正则表达式

node.js - 如何通过#MicrosoftGraph 上传 Excel 文件的内容

javascript - Node.js 正则表达式不起作用

asp.net - 处理 EF4 中模型的默认值

javascript - 将带有普通对象数组的普通对象展开为平面普通对象

javascript - 如果变量未定义,try/catch(其中一种)是防止错误发生的最佳方法吗?

javascript - 在需要时加载谷歌地图 api 脚本? jquery.getScript()?

javascript - Async/Await 未按照 for 循环语句的预期工作

java - 使用 hibernate 注释将枚举映射到表

java - 防止 NaN 被 Hibernate 持久化