续集 "where" 引用包含模型列
Sequels "where" referring to include model column
我有以下使用 node.js 和 Sequelize (@3.3.2) 的模型:
module.exports = function (sequelize, DataTypes) {
var Topic = sequelize.define("Topic", {
name: DataTypes.STRING,
}, {
classMethods: {
associate: function (models) {
Topic.hasMany(models.Section, {as: 'sections'});
}
}
});
return Topic;
};
module.exports = function (sequelize, DataTypes) {
var Section = sequelize.define("Section", {
previewImg: DataTypes.STRING,
description: {
type: DataTypes.TEXT,
allowNull: true
}
}, {
classMethods: {
associate: function (models) {
Section.belongsTo(models.Topic);
Section.belongsToMany(models.Domain, {through: 'SectionDomainsExclusive', as: 'exclusiveDomains'});
}
}
});
return Section;
};
module.exports = function (sequelize, DataTypes) {
var Domain = sequelize.define("Domain", {
name: {
type: DataTypes.STRING,
allowNull: false
}
});
return Domain;
};
所以一个主题有很多部分,一个部分可以有一个 "exclusive" 域的列表。
所需的功能是:
If there are no exclusive domains (no relation), the domain is visible in all these domains. If there are one or more exclusive domains assigned, the section shall be visible to only these domains
现在我想检索主题以及可见的部分:
models.Topic.findAll({
include: [{
model: models.Section,
as: 'sections',
include: [{
model: models.Domain,
required: false,
as: 'exclusiveDomains',
}],
where: {
'exclusiveDomains.id': {
$or: {
$eq: 3,
$eq: null
}
}
}
}]
}).then(function (objList) {
///...
});
所以我想在域上使用别名 "exclusiveDomains" 进行 LEFT JOIN(必需:false),然后在 WHERE 整个查询结果中没有找到独占域(NULL)或有该部分对域 3 可见,这是我的域。
我不能将 WHERE 部分放在包含中,因为这只会限制连接的结果,如果该部分没有独占域列表或者我的域 (3) 不在列表。
不幸的是,Sequelize 总是将 "section." 放在我的 WHERE 前面,所以这会导致以下错误:
Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such column: sections.exclusiveDomains.id
SQL-字符串应如下所示:
SELECT `Topic`.`id`,
`sections`.`id` AS
`sections.id`,
`sections.exclusiveDomains`.`id` AS
`sections.exclusiveDomains.id`,
`sections.exclusiveDomains.SectionDomainsExclusive`.`sectionid` AS
`sections.exclusiveDomains.SectionDomainsExclusive.SectionId`,
`sections.exclusiveDomains.SectionDomainsExclusive`.`domainid` AS
`sections.exclusiveDomains.SectionDomainsExclusive.DomainId`
FROM `topics` AS `Topic`
INNER JOIN `sections` AS `sections`
ON `Topic`.`id` = `sections`.`topicid`
INNER JOIN `sectionusergroup` AS `sections.usergroups.SectionUsergroup`
ON `sections`.`id` = `sections.usergroups.SectionUsergroup`.`sectionid`
LEFT OUTER JOIN `sectiondomainsexclusive` AS `sections.exclusiveDomains.SectionDomainsExclusive`
ON `sections`.`id` = `sections.exclusiveDomains.SectionDomainsExclusive`.`sectionid`
LEFT OUTER JOIN `domains` AS `sections.exclusiveDomains`
ON `sections.exclusiveDomains`.`id` = `sections.exclusiveDomains.SectionDomainsExclusive`.`domainid`
WHERE (`sections.exclusiveDomains`.`id` = 3 OR `sections.exclusiveDomains`.`id` IS NULL);
不幸的是我无法获取sections.exclusiveDomains
.id
部分,因为Sequelize总是把section放在前面,所以看起来像sections
.exclusiveDomains.id
,这显然是错误的。
我能做什么?
感谢您的支持!
我发现原始查询是最好的方法。
where:["sections.exclusiveDomains.id = 3 OR sections.exclusiveDomains.id is null", null]
可能有一种更简洁的方法来提取完全限定名称,但目前这似乎是一种享受!
我有以下使用 node.js 和 Sequelize (@3.3.2) 的模型:
module.exports = function (sequelize, DataTypes) {
var Topic = sequelize.define("Topic", {
name: DataTypes.STRING,
}, {
classMethods: {
associate: function (models) {
Topic.hasMany(models.Section, {as: 'sections'});
}
}
});
return Topic;
};
module.exports = function (sequelize, DataTypes) {
var Section = sequelize.define("Section", {
previewImg: DataTypes.STRING,
description: {
type: DataTypes.TEXT,
allowNull: true
}
}, {
classMethods: {
associate: function (models) {
Section.belongsTo(models.Topic);
Section.belongsToMany(models.Domain, {through: 'SectionDomainsExclusive', as: 'exclusiveDomains'});
}
}
});
return Section;
};
module.exports = function (sequelize, DataTypes) {
var Domain = sequelize.define("Domain", {
name: {
type: DataTypes.STRING,
allowNull: false
}
});
return Domain;
};
所以一个主题有很多部分,一个部分可以有一个 "exclusive" 域的列表。 所需的功能是:
If there are no exclusive domains (no relation), the domain is visible in all these domains. If there are one or more exclusive domains assigned, the section shall be visible to only these domains
现在我想检索主题以及可见的部分:
models.Topic.findAll({
include: [{
model: models.Section,
as: 'sections',
include: [{
model: models.Domain,
required: false,
as: 'exclusiveDomains',
}],
where: {
'exclusiveDomains.id': {
$or: {
$eq: 3,
$eq: null
}
}
}
}]
}).then(function (objList) {
///...
});
所以我想在域上使用别名 "exclusiveDomains" 进行 LEFT JOIN(必需:false),然后在 WHERE 整个查询结果中没有找到独占域(NULL)或有该部分对域 3 可见,这是我的域。
我不能将 WHERE 部分放在包含中,因为这只会限制连接的结果,如果该部分没有独占域列表或者我的域 (3) 不在列表。
不幸的是,Sequelize 总是将 "section." 放在我的 WHERE 前面,所以这会导致以下错误:
Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such column: sections.exclusiveDomains.id
SQL-字符串应如下所示:
SELECT `Topic`.`id`,
`sections`.`id` AS
`sections.id`,
`sections.exclusiveDomains`.`id` AS
`sections.exclusiveDomains.id`,
`sections.exclusiveDomains.SectionDomainsExclusive`.`sectionid` AS
`sections.exclusiveDomains.SectionDomainsExclusive.SectionId`,
`sections.exclusiveDomains.SectionDomainsExclusive`.`domainid` AS
`sections.exclusiveDomains.SectionDomainsExclusive.DomainId`
FROM `topics` AS `Topic`
INNER JOIN `sections` AS `sections`
ON `Topic`.`id` = `sections`.`topicid`
INNER JOIN `sectionusergroup` AS `sections.usergroups.SectionUsergroup`
ON `sections`.`id` = `sections.usergroups.SectionUsergroup`.`sectionid`
LEFT OUTER JOIN `sectiondomainsexclusive` AS `sections.exclusiveDomains.SectionDomainsExclusive`
ON `sections`.`id` = `sections.exclusiveDomains.SectionDomainsExclusive`.`sectionid`
LEFT OUTER JOIN `domains` AS `sections.exclusiveDomains`
ON `sections.exclusiveDomains`.`id` = `sections.exclusiveDomains.SectionDomainsExclusive`.`domainid`
WHERE (`sections.exclusiveDomains`.`id` = 3 OR `sections.exclusiveDomains`.`id` IS NULL);
不幸的是我无法获取sections.exclusiveDomains
.id
部分,因为Sequelize总是把section放在前面,所以看起来像sections
.exclusiveDomains.id
,这显然是错误的。
我能做什么?
感谢您的支持!
我发现原始查询是最好的方法。
where:["sections.exclusiveDomains.id = 3 OR sections.exclusiveDomains.id is null", null]
可能有一种更简洁的方法来提取完全限定名称,但目前这似乎是一种享受!