我将如何使用 Sequelize 查找关系中的列满足条件的所有人?
例如,查找作者姓氏为“Hitchcock”的所有书籍。book 模式包含与 Author 表的 hasOne 关系。
编辑:我了解如何使用原始 SQL 查询来完成此操作,但正在寻找另一种方法
我将如何使用 Sequelize 查找关系中的列满足条件的所有人?
例如,查找作者姓氏为“Hitchcock”的所有书籍。book 模式包含与 Author 表的 hasOne 关系。
编辑:我了解如何使用原始 SQL 查询来完成此操作,但正在寻找另一种方法
这是一个工作示例,说明如何使用 Sequelize 以具有特定姓氏的方式获取所有Books
内容Author
。它看起来比实际复杂一些,因为我正在定义模型、关联它们、与数据库同步(以创建它们的表),然后在这些新表中创建虚拟数据。查找findAll
代码中间的 ,具体查看您所追求的内容。
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 提出的方法不起作用。
现在您需要执行以下操作:
return Book.findAll({
where: {
'$Authors.lastName$': 'Testerson'
},
include: [
{model: Author, as: Author.tableName}
]
});
对于文档!
检查急切加载部分
https://sequelize.org/master/manual/eager-loading.html
对于以上答案!您可以在以下标题的文档中找到它
顶层复杂的 where 子句
为了获得涉及嵌套列的顶级 WHERE 子句,Sequelize 提供了一种引用嵌套列的方法:'$nested.column$' 语法。
例如,它可用于将包含模型中的 where 条件从 ON 条件移动到顶级 WHERE 子句。
User.findAll({
where: {
'$Instruments.size$': { [Op.ne]: 'small' }
},
include: [{
model: Tool,
as: 'Instruments'
}]
});
生成的 SQL:
SELECT
`user`.`id`,
`user`.`name`,
`Instruments`.`id` AS `Instruments.id`,
`Instruments`.`name` AS `Instruments.name`,
`Instruments`.`size` AS `Instruments.size`,
`Instruments`.`userId` AS `Instruments.userId`
FROM `users` AS `user`
LEFT OUTER JOIN `tools` AS `Instruments` ON
`user`.`id` = `Instruments`.`userId`
WHERE `Instruments`.`size` != 'small';
为了更好地理解内部 where 选项(在 include 中使用)、带和不带 required 选项以及使用 $nested.column$ 语法的顶级 where 之间的所有差异,下面我们为您提供了四个示例:
// Inner where, with default `required: true`
await User.findAll({
include: {
model: Tool,
as: 'Instruments',
where: {
size: { [Op.ne]: 'small' }
}
}
});
// Inner where, `required: false`
await User.findAll({
include: {
model: Tool,
as: 'Instruments',
where: {
size: { [Op.ne]: 'small' }
},
required: false
}
});
// Top-level where, with default `required: false`
await User.findAll({
where: {
'$Instruments.size$': { [Op.ne]: 'small' }
},
include: {
model: Tool,
as: 'Instruments'
}
});
// Top-level where, `required: true`
await User.findAll({
where: {
'$Instruments.size$': { [Op.ne]: 'small' }
},
include: {
model: Tool,
as: 'Instruments',
required: true
}
});
生成的 SQL,按顺序:
-- Inner where, with default `required: true`
SELECT [...] FROM `users` AS `user`
INNER JOIN `tools` AS `Instruments` ON
`user`.`id` = `Instruments`.`userId`
AND `Instruments`.`size` != 'small';
-- Inner where, `required: false`
SELECT [...] FROM `users` AS `user`
LEFT OUTER JOIN `tools` AS `Instruments` ON
`user`.`id` = `Instruments`.`userId`
AND `Instruments`.`size` != 'small';
-- Top-level where, with default `required: false`
SELECT [...] FROM `users` AS `user`
LEFT OUTER JOIN `tools` AS `Instruments` ON
`user`.`id` = `Instruments`.`userId`
WHERE `Instruments`.`size` != 'small';
-- Top-level where, `required: true`
SELECT [...] FROM `users` AS `user`
INNER JOIN `tools` AS `Instruments` ON
`user`.`id` = `Instruments`.`userId`
WHERE `Instruments`.`size` != 'small';
这让我们很好地了解了join是如何完成的!