Node.js的后续关联包括

这是一个错误,当我查询模型(短版本):

var User = db.define('User', { login: Sequelize.STRING(16), password: Sequelize.STRING, }); var Group = db.define('Group', { name: Sequelize.STRING, }); var GroupSection = db.define('GroupSection', { name: Sequelize.STRING, }); Group.belongsTo(GroupSection, { as: 'GroupSection', foreignKey: 'GroupSectionId' }); GroupSection.hasMany(Group, { as: 'Groups', foreignKey: 'GroupSectionId' }); Group.belongsTo(Group, { as: 'ParentGroup', foreignKey: 'ParentGroupId' }); Group.hasMany(Group, { as: 'ChildGroups', foreignKey: 'ParentGroupId' }); User.belongsToMany(Group, { as: 'Groups', through: 'UsersToGroups' }); Group.belongsToMany(User, { as: 'Users', through: 'UsersToGroups' }); 

这个查询工作正常(注意包括里面包括):

 User.findOne({ include: [{ model: Group, as: 'Groups', where: { name: 'Group name', }, include: [{ model: GroupSection, as: 'GroupSection', }] }] }).then(function(user) { // some code }) 

但是这个查询给出了错误(只有“where”参数被添加到内部包含中):

 User.findOne({ include: [{ model: Group, as: 'Groups', where: { name: 'Group name', }, include: [{ model: GroupSection, as: 'GroupSection', where: { name: 'Some section name', }, }] }] }).then(function(user) { // some code }) 

上面的代码给出错误:
未处理的拒绝SequelizeDatabaseError:缺less表“Groups”的FROM-clause条目

我检查了它产生的SQL代码,我可以通过不使用内部where子句来修复这个问题,但是在where子句中添加了一些原始代码。 我该如何做这样的事情:

 User.findOne({ include: [{ model: Group, as: 'Groups', where: { name: 'Admin', $somethin_i_need$: 'raw sql goes here', }, include: [{ model: GroupSection, as: 'GroupSection', }] }] }).then(function(user) { // some code }) 

ADDED(代码被一些在线服务所美化):

代码生成没有内部的地方(工作正常):

 SELECT "User".*, "groups"."id" AS "Groups.id", "groups"."name" AS "Groups.name", "groups"."createdat" AS "Groups.createdAt", "groups"."updatedat" AS "Groups.updatedAt", "groups"."groupsectionid" AS "Groups.GroupSectionId", "groups"."parentgroupid" AS "Groups.ParentGroupId", "Groups.UsersToGroups"."createdat" AS "Groups.UsersToGroups.createdAt", "Groups.UsersToGroups"."updatedat" AS "Groups.UsersToGroups.updatedAt", "Groups.UsersToGroups"."groupid" AS "Groups.UsersToGroups.GroupId", "Groups.UsersToGroups"."userid" AS "Groups.UsersToGroups.UserId", "Groups.GroupSection"."id" AS "Groups.GroupSection.id", "Groups.GroupSection"."name" AS "Groups.GroupSection.name", "Groups.GroupSection"."createdat" AS "Groups.GroupSection.createdAt", "Groups.GroupSection"."updatedat" AS "Groups.GroupSection.updatedAt" FROM (SELECT "User"."id", "User"."login", "User"."password", "User"."createdat", "User"."updatedat" FROM "users" AS "User" WHERE (SELECT "userstogroups"."groupid" FROM "userstogroups" AS "UsersToGroups" INNER JOIN "groups" AS "Group" ON "userstogroups"."groupid" = "Group"."id" WHERE ( "User"."id" = "userstogroups"."userid" ) LIMIT 1) IS NOT NULL LIMIT 1) AS "User" INNER JOIN ("userstogroups" AS "Groups.UsersToGroups" INNER JOIN "groups" AS "Groups" ON "groups"."id" = "Groups.UsersToGroups"."groupid") ON "User"."id" = "Groups.UsersToGroups"."userid" AND "groups"."name" = 'Group name' LEFT OUTER JOIN "groupsections" AS "Groups.GroupSection" ON "groups"."groupsectionid" = "Groups.GroupSection"."id"; 

代码生成与内部在哪里(错误的SQL生成):

 SELECT "User".*, "groups"."id" AS "Groups.id", "groups"."name" AS "Groups.name", "groups"."createdat" AS "Groups.createdAt", "groups"."updatedat" AS "Groups.updatedAt", "groups"."groupsectionid" AS "Groups.GroupSectionId", "groups"."parentgroupid" AS "Groups.ParentGroupId", "Groups.UsersToGroups"."createdat" AS "Groups.UsersToGroups.createdAt", "Groups.UsersToGroups"."updatedat" AS "Groups.UsersToGroups.updatedAt", "Groups.UsersToGroups"."groupid" AS "Groups.UsersToGroups.GroupId", "Groups.UsersToGroups"."userid" AS "Groups.UsersToGroups.UserId" FROM (SELECT "User"."id", "User"."login", "User"."password", "User"."createdat", "User"."updatedat", "Groups.GroupSection"."id" AS "Groups.GroupSection.id", "Groups.GroupSection"."name" AS "Groups.GroupSection.name", "Groups.GroupSection"."createdat" AS "Groups.GroupSection.createdAt", "Groups.GroupSection"."updatedat" AS "Groups.GroupSection.updatedAt" FROM "users" AS "User" INNER JOIN "groupsections" AS "Groups.GroupSection" ON "groups"."GroupSectionId" = "Groups.GroupSection"."id" AND "Groups.GroupSection"."name" = 'Section name' WHERE (SELECT "userstogroups"."groupid" FROM "userstogroups" AS "UsersToGroups" INNER JOIN "groups" AS "Group" ON "userstogroups"."groupid" = "Group"."id" WHERE ( "User"."id" = "userstogroups"."userid" ) LIMIT 1) IS NOT NULL LIMIT 1) AS "User" INNER JOIN ("userstogroups" AS "Groups.UsersToGroups" INNER JOIN "groups" AS "Groups" ON "groups"."id" = "Groups.UsersToGroups"."groupid") ON "User"."id" = "Groups.UsersToGroups"."userid" AND "groups"."name" = 'Group name'; 

请注意真正需要什么

我不需要具有没有组的组或没有组的段的logging等等。 例如,在用户被发现后(用户决定进入search结果),将用户组附加到用户身上。 这意味着我需要这个“where”子句在用户模型上(与对象中的第一个“包含”键在同一级别),但是需要检查几个表(我的真实数据库更复杂) 。

我有类似的错误。 而我没有find我的问题的任何答案。 但是我做到了。 我不知道它是否也适用于你,但我写我的解决scheme。

User.findOne({ include: [{ model: Group, as: 'Groups', where: { name: 'Group name', }, include: [{ model: GroupSection, as: 'GroupSection', required: false, where: { name: 'Some section name', }, }] }] }).then(function(user) { // some code })

为什么这对我有用,应该为你工作? 当你忽略了最后一个子查询中的位置时,默认情况下required值为false。 当你设置的时候where默认情况下required值是true。 这引导我到这个解决scheme。

从docs作为确认:

[options.include []。where] Where子句应用于子模型。 请注意,除非您明确设置了required:false,否则这会将热切负载转换为内部连接

[options.include []。required]如果为true,则转换为内部联接,这意味着如果父级模型有任何匹配的子级,则只会加载父级模型。 如果设置了include.where,则为true,否则为false。

总之内连接有一些问题。 而当你设置属性,那么这将添加子查询作为内部联接,除非你设置required:false