Sequelize.js 对导致子查询的关联进行续集

Sequelize.js 对导致子查询的关联进行续集,sequelize.js,Sequelize.js,我的应用程序中有一个功能,可以作为博客文章的活动提要(类似于Tumblr)。在这个页面的路径中,我的主要查询是在我的blog post表上,我将包含额外元信息和注释的表加入到这个表中,以获得每个blog post的一张功能齐全的卡片。这些表格来自我的分类(对文章进行分类)、主题(感兴趣的主题)、附加的文件(文件/图像)和评论(评论文章)表格 通过我当前的查询,我可以毫无问题地连接所有这些表,但是我遇到了一个问题,我的where子句与博客文章上的父查询关联。当文件和注释表连接在一起时,会出现一个子

我的应用程序中有一个功能,可以作为博客文章的活动提要(类似于Tumblr)。在这个页面的路径中,我的主要查询是在我的blog post表上,我将包含额外元信息和注释的表加入到这个表中,以获得每个blog post的一张功能齐全的卡片。这些表格来自我的分类(对文章进行分类)、主题(感兴趣的主题)、附加的文件(文件/图像)和评论(评论文章)表格

通过我当前的查询,我可以毫无问题地连接所有这些表,但是我遇到了一个问题,我的where子句与博客文章上的父查询关联。当
文件
注释
表连接在一起时,会出现一个子查询,我认为这与博客文章和这些表之间的关联类型有关,并以某种方式影响结果

提供了架构和关联:

SELECT `blog_post`.*, `user`.`user_id`, `user`.`first_name`, `user`.`last_name`, `topic`.`topic_id`, `topic`.`topic`, `category`.`category_id`, `category`.`category_name`, `file`.`file_id`, `file`.`file`, `comment`.`comment_id`, `comment`.`comment`, `comment`.`blog_id`, `comment`.`user_id`, `comment`.`created_at`, `comment`.`updated_at`, `comment`.`blog_id`, `comment`.`user_id`, `comment.user`.`user_id`
FROM (SELECT `blog_post.`blog_id`, `blog_post.`date`, `blog_post.`title`, `blog_post.`content`, `blog_post.`topic_id`, `blog_post.`category_id`, `blog_post.`user_id`, `blog_post.`created_at`, `blog_post.`updated_at`, `blog_post.`user_id`, `blog_post.`topic_id`, `blog_post.`category_id` FROM `blog_post LIMIT 10) AS `blog_post 
LEFT OUTER JOIN `user` AS `user` ON `blog_post.`user_id` = `user`.`user_id` AND `user`.`organization_id` = 1 
LEFT OUTER JOIN `topic` AS `topic` ON `blog_post.`topic_id` = `topic`.`topic_id` 
LEFT OUTER JOIN `category` AS `category` ON `blog_post.`category_id` = `category`.`category_id` 
LEFT OUTER JOIN `file` AS `file` ON `blog_post.`blog_id` = `file`.`blog_id` 
LEFT OUTER JOIN `comment` AS `comment` ON `blog_post.`blog_id` = `comment`.`blog_id` 
LEFT OUTER JOIN `user` AS `comment.user` ON `comment`.`user_id` = `comment.user`.`user_id` 
ORDER BY date DESC;
博客文章

  • 博客id
  • 日期
  • 内容
  • 类别识别码
  • 主题id
联想

  • belongsTo(用户,{foreignKey:'user_id'})
  • belongsTo(类别,{foreignKey:'category_id'})
  • belongsTo(主题,{foreignKey:'topic_id'})
  • hasMany(文件,{foreignKey:'blog_id'})
  • hasMany(评论,{foreignKey:'blog_id'})
用户

  • 用户id
  • 名字
  • 组织id
类别

  • 类别识别码
  • 类别
联想

  • hasMany(blog_post,{foreignKey:'category_id'})
主题

  • 主题id
  • 话题
联想

  • hasMany(blog_post,{foreignKey:'topic_id'})
文件和注释表在其与此问题相关的架构文件中没有关联

提供的是查询(请不要使用上面的别名列名)

使用子查询更新:

SELECT `blog_post`.*, `user`.`user_id`, `user`.`first_name`, `user`.`last_name`, `topic`.`topic_id`, `topic`.`topic`, `category`.`category_id`, `category`.`category_name`, `file`.`file_id`, `file`.`file`, `comment`.`comment_id`, `comment`.`comment`, `comment`.`blog_id`, `comment`.`user_id`, `comment`.`created_at`, `comment`.`updated_at`, `comment`.`blog_id`, `comment`.`user_id`, `comment.user`.`user_id`
FROM (SELECT `blog_post.`blog_id`, `blog_post.`date`, `blog_post.`title`, `blog_post.`content`, `blog_post.`topic_id`, `blog_post.`category_id`, `blog_post.`user_id`, `blog_post.`created_at`, `blog_post.`updated_at`, `blog_post.`user_id`, `blog_post.`topic_id`, `blog_post.`category_id` FROM `blog_post LIMIT 10) AS `blog_post 
LEFT OUTER JOIN `user` AS `user` ON `blog_post.`user_id` = `user`.`user_id` AND `user`.`organization_id` = 1 
LEFT OUTER JOIN `topic` AS `topic` ON `blog_post.`topic_id` = `topic`.`topic_id` 
LEFT OUTER JOIN `category` AS `category` ON `blog_post.`category_id` = `category`.`category_id` 
LEFT OUTER JOIN `file` AS `file` ON `blog_post.`blog_id` = `file`.`blog_id` 
LEFT OUTER JOIN `comment` AS `comment` ON `blog_post.`blog_id` = `comment`.`blog_id` 
LEFT OUTER JOIN `user` AS `comment.user` ON `comment`.`user_id` = `comment.user`.`user_id` 
ORDER BY date DESC;

您可能需要在
findAll
方法的
options
对象中添加
subQuery:false
。但是,sequelize文档中没有此选项,您可以在中找到它。如果未设置,则默认为
true
,当查询中存在
LIMIT
选项时,会导致添加您提到的子查询。

。我对where语句对象使用了子查询:false

比如:--

但您需要记住,如果在where语句中使用内部表 然后,您将不得不使用类似于:--

因为如果不使用“$”,它将创建类似以下的自动:

abcd(主表).childrens.id=12

因此,要检查childerns id上的id值,您将 必须使用“$”


是的。希望它对您有用。

您得到了什么子查询?你能粘贴生成的SQL吗?@piotrbienias我添加了生成的SQL查看下面的答案并告诉我它是否解决了问题哇,我很惊讶这没有文档记录,很棒的发现!但是,删除子查询的结果似乎多次重复记录,我正在尝试调试。回到我最初的想法,根据删除子查询的结果,我认为子查询与我的问题无关。子查询实际上允许我限制查询中捕获的blog_帖子的数量,并连接子查询上的表(这是预期的结果),限制blog_帖子记录并将相关信息附加到这些帖子上。这使我认为,包括不同类型的关联是导致问题的原因。这听起来合乎逻辑吗?在深入挖掘之后,我的问题的解决方案是在子查询中包含
order
属性。你知道这是怎么做到的吗?
{
include:[{model:childrens,attributes:['id','firstName']}],
where:{'$childrens.id$':12}
subQuery:false
}
where:{'$childrens.id$':12}