- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这是我查询模型(简短版本)时的错误吗:
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' });
这个查询工作正常(注意 include ininclude):
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:缺少表“Groups”的 FROM 子句条目
我检查了它生成的 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
})
没有内部 where 生成的代码(工作正常):
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";
通过内部 where 生成的代码(生成了错误的 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';
注意真正需要什么:
我不需要包含没有组的用户或没有部分的组等的记录。例如。在找到该用户后,将组附加到用户(并决定将其放入结果中)。这意味着我需要这个“where”子句在用户模型上(与对象中的第一个“inclusion”键处于同一级别),但它需要检查几个表(我的真实数据库更复杂) .
最佳答案
我有类似的错误。而且我没有找到我的问题的任何答案。但我让它工作。我不知道它是否也适用于您,但我写了我的解决方案。
请尝试在最后的 include 中添加 required:false
属性:
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
})
为什么这对我有用,对你也应该有用?当您在最后一个子查询中省略 where
时,默认情况下 required
的值为 false。当您设置 where
时,默认情况下 required
的值为 true。这引导我找到了这个解决方案。
来自 docs作为确认:
[options.include[].where] Where clauses to apply to the child models. Note that this converts the eager load to an inner join, unless you explicitly set required: false
和
[options.include[].required] If true, converts to an inner join, which means that the parent model will only be loaded if it has any matching children. True if include.where is set, false otherwise.
简而言之,内部连接存在一些问题。当您设置 where
属性时,除非您设置了 required:false
,否则这会将子查询添加为内部联接。
关于Node.js sequelize 关联包括,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30264438/
127.0.0.1:8000/api/仅包含来自第二个应用程序的 url,但我将两个 url 模块链接到相同的模式。甚至有可能做到这一点吗? 第一个应用程序: from django.urls imp
我目前正在学习 ColdFusion。我有 PHP 背景,对此我有点困惑。 我有一个选择菜单,我希望将选项保存在不同的文件中。 (例如 options.cfm)当我调用文件时,我想在选择菜单中包含选项
字符串: "75,000", "is", "95%", "or", "95/100" "of", "monthly", "income" o/p: "is","%, "or", "/", "of",
我有 4 个 javascript 文件(每个文件对应一个 HTML 文件),所有 4 个文件中的 3 个函数都是相同的。我想找到一个顺利的解决方案,我可以以某种方式分别包含这 3 个函数...是否可
我在 PHP 中有这种情况,其中 include在一台服务器上被遗漏,但在另一台服务器上没有(我没有设置服务器,所以我不能告诉你更多;我不是真正的 devops 人,所以这就是我在这里问的原因)。两台
这是一个模式文件,midi.xsd定义类型,note ,用于存储 MIDI 音符值: 这是另一个模式文件,octaves.xsd使用
我想备份以下文件夹 /home /etc /usr/local /root /var /boot 并排除 /var/tmp /var/run /var/lock /home/*/.thumbnails
如何重新编码具有许多值(包括缺失值)的数值变量,以获得数字 0:n-1哪里n是唯一值的数量,包括 NA ,整齐? 例子: df 1 1000 0 2 1000 0 3 N
选择元素的 html(包括在内)的最佳方法是什么?例如: This is just a test. 而$('#testDiv').html()返回"This is just a test."
我正在尝试设置Varnish来处理本地环境中的ESI包含。 我在虚拟机中运行 Varnish ,内容在主机上运行。 我有两个文件“index.html”和“test.html”。它们都存储在apach
我有以下内容,并且想要检索“ FromEmail”不为空的数据 Simple email@gma
欧海,我正在编写一个小型 PHP 应用程序,使用一个单独的 config.php 文件和一个functions.php,其中包含我将在应用程序中使用的所有自定义函数。现在,我真的必须在每个函数中包含
我知道可以将 JavaScript 放在一个特定的 .js 文件中,然后通过执行以下操作将其包含在任何页面中...... 我注意到,对于包含的这些 .js 文件: 它们实际上不必以 .js 结尾 其
我使用 gwt UIBinder 添加了一些项目到我的 ComboBox。 --select one-- Dispute Referral Form Dispute Settlement Clause
我可以将一个 first.c 文件包含到另一个 second.c 中吗? (我正在做一些套接字编程,以将服务器收到的消息存储在链接列表中,因此在第一个程序中,我尝试保留链接列表和第二个程序套接字编程文
我有一个简单的 Spring MVC 数据项目设置,我试图选择 Admin 中尚不存在的用户列表。 table 。这是我的存储库方法 SELECT u FROM User u WHERE u.id N
在 bash 脚本中,使用什么实用程序以及如何删除两个字符串之间的文本,包括字符串。 原文: (ABC blah1)blah 2(def blah 5)blah 7)(DEF blah 8)blah
我有这个 BST 问题,我试图用 Java 解决,但我不知道为什么它不起作用。问题是: 二叉搜索树 (BST) 是一种二叉树,其中每个值节点大于或等于该节点的所有节点中的值左子树并且小于该树中所有节点
我有一个字符串,其中包含“Dollars”和“Cents”符号。我想删除它们。我试过了 string.replaceAll("[\"\\u00A2\" $]", "") 但它不起作用。正确的做法是什么
我在 stories 和 tags 之间有一个多对多的关系,为保存关系而创建的表是 taxonomies。我想搜索所有具有所有给定标签的故事。 到目前为止我使用的查询是这个,当然它对我不起作用,它返回
我是一名优秀的程序员,十分优秀!