Node.js 基于关联的续集查找
如何使用Sequelize查找关系中某列满足条件的所有人 一个例子是找到所有作者姓“希区柯克”的书。图书模式包含与作者表的hasOne关系Node.js 基于关联的续集查找,node.js,sequelize.js,Node.js,Sequelize.js,如何使用Sequelize查找关系中某列满足条件的所有人 一个例子是找到所有作者姓“希区柯克”的书。图书模式包含与作者表的hasOne关系 编辑:我了解如何使用原始SQL查询来实现这一点,但正在寻找另一种方法这里有一个工作示例,说明如何使用户续集以获得所有书籍,由具有特定姓氏的作者完成。它看起来比实际情况要复杂得多,因为我正在定义模型,关联它们,与数据库同步(创建它们的表),然后在这些新表中创建虚拟数据。在代码的中间寻找 FUNDALL < /代码>,以具体查看您所追求的内容。 modu
编辑:我了解如何使用原始SQL查询来实现这一点,但正在寻找另一种方法这里有一个工作示例,说明如何使用户续集以获得所有
书籍
,由具有特定姓氏的作者
完成。它看起来比实际情况要复杂得多,因为我正在定义模型,关联它们,与数据库同步(创建它们的表),然后在这些新表中创建虚拟数据。在代码的中间寻找<代码> FUNDALL < /代码>,以具体查看您所追求的内容。
module.exports = function(sequelize, DataTypes) {
var Author = sequelize.define('Author', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true
},
firstName: {
type: DataTypes.STRING
},
lastName: {
type: DataTypes.STRING
}
})
var Book = sequelize.define('Book', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true
},
title: {
type: DataTypes.STRING
}
})
var firstAuthor;
var secondAuthor;
Author.hasMany(Book)
Book.belongsTo(Author)
Author.sync({ force: true })
.then(function() {
return Book.sync({ force: true });
})
.then(function() {
return Author.create({firstName: 'Test', lastName: 'Testerson'});
})
.then(function(author1) {
firstAuthor=author1;
return Author.create({firstName: 'The Invisible', lastName: 'Hand'});
})
.then(function(author2) {
secondAuthor=author2
return Book.create({AuthorId: firstAuthor.id, title: 'A simple book'});
})
.then(function() {
return Book.create({AuthorId: firstAuthor.id, title: 'Another book'});
})
.then(function() {
return Book.create({AuthorId: secondAuthor.id, title: 'Some other book'});
})
.then(function() {
// This is the part you're after.
return Book.findAll({
where: {
'Authors.lastName': 'Testerson'
},
include: [
{model: Author, as: Author.tableName}
]
});
})
.then(function(books) {
console.log('There are ' + books.length + ' books by Test Testerson')
});
}
在Sequilize(5.9.0)的最新版本中,@c.hill提出的方法不起作用 现在,您需要执行以下操作:
returnbook.findAll({
其中:{
“$Authors.lastName$”:“Testerson”
},
包括:[
{model:Author,as:Author.tableName}
]
});
查看文档
检查快速加载部分
以上的答案!您可以在以下标题的文档中找到它
顶级复杂where子句
从文档:
要获取涉及嵌套列的顶级WHERE子句,Sequelize提供了一种引用嵌套列的方法:“$nested.column$”语法
例如,可以使用它将where条件从包含的模型从ON条件移动到顶级where子句
User.findAll({
其中:{
“$Instruments.size$”:{[Op.ne]:'small'}
},
包括:[{
型号:工具,
as:“仪器”
}]
});
生成的SQL:
选择
`用户`.`id`,
`用户`.`name`,
`Instruments`.`id`作为`Instruments.id`,
`Instruments`.`name`作为`Instruments.name`,
`仪器`.`size`作为`仪器.尺寸`,
`Instruments`.`userId`作为`Instruments.userId`
从“用户”到“用户”`
左外连接“工具”为“仪器”
`用户`.`id`=`Instruments`.`userId`
其中'Instruments'。'size`!='小';
为了更好地理解内部where选项(在include中使用)与使用$nested.column$语法的顶级where之间的所有差异,下面我们为您提供了四个示例:
//内部where,默认为'required:true`
等待User.findAll({
包括:{
型号:工具,
例如:“文书”,
其中:{
大小:{[Op.ne]:'small'}
}
}
});
//内部where,`required:false`
等待User.findAll({
包括:{
型号:工具,
例如:“文书”,
其中:{
大小:{[Op.ne]:'small'}
},
必填项:false
}
});
//顶层,其中默认值为“必需”:false`
等待User.findAll({
其中:{
“$Instruments.size$”:{[Op.ne]:'small'}
},
包括:{
型号:工具,
as:“仪器”
}
});
//顶层,其中,`required:true`
等待User.findAll({
其中:{
“$Instruments.size$”:{[Op.ne]:'small'}
},
包括:{
型号:工具,
例如:“文书”,
必填项:true
}
});
生成的SQL,顺序为:
--内部where,默认为'required:true`
从“用户”中选择[…]作为“用户”`
将“工具”作为“仪器”的内部连接
`用户`.`id`=`Instruments`.`userId`
和“仪器”。“尺寸”!=”小';
--内部where,`required:false`
从“用户”中选择[…]作为“用户”`
左外连接“工具”为“仪器”
`用户`.`id`=`Instruments`.`userId`
和“仪器”。“尺寸”!=”小';
--顶层,其中默认值为“必需”:false`
从“用户”中选择[…]作为“用户”`
左外连接“工具”为“仪器”
`用户`.`id`=`Instruments`.`userId`
其中'Instruments'。'size`!='小';
--顶层,其中,`required:true`
从“用户”中选择[…]作为“用户”`
将“工具”作为“仪器”的内部连接
`用户`.`id`=`Instruments`.`userId`
其中'Instruments'。'size`!='小';
这让我们很好地了解了加入是如何完成的 这对我不起作用,我认为在sequelize 2.0中,where子句必须放在include数组成员中,例如
include:[{model:…,where:…}]
,答案给了我希望,这在某种程度上是可能的,因此我寻求一个解决方案:)我可以确认,从Sequelize 2.0开始,where
子句必须在include
中,就像:include:[{model:…},where:{'Author.lastName:'Testerson}]
where子句中不需要作者:{'lastName':'Testerson'}
putwhere
insideinclude
生成如下查询:LEFT-OUTER-JOIN-Authors-ON-Books.authorId=Authors.id和Authors.lastName='Testerson'
,结果与Books.authorId=Authors.id上的左外部联接Authors有很大不同。。。其中Authors.lastName='Testerson'。我不知道如何使用sequelize实现后一个查询。我在哪里可以找到这方面的文档?我没有找到任何文档,但这非常有效@lordvcs在这里:当我用Sequelize花2个小时来完成这个任务时,我错过了好的普通SQL