node.js 基于关联的 Sequelize 查找

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18838433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:37:51  来源:igfitidea点击:

Sequelize find based on association

node.jssequelize.js

提问by futbolpal

How would I use Sequelize to find all people where a column in the relation satisfies a condition?

我将如何使用 Sequelize 查找关系中的列满足条件的所有人?

An example would be to find all Books whose author's last name is 'Hitchcock'. The book schema contains a hasOne relation with the Author's table.

例如,查找作者姓氏为“希区柯克”的所有书籍。book 模式包含一个与 Author 表的 hasOne 关系。

Edit: I understand how this could be done with a raw SQL query, but looking for another approach

编辑:我了解如何使用原始 SQL 查询完成此操作,但正在寻找另一种方法

采纳答案by c.hill

Here's a working sample of how to user Sequelize to get all Booksby an Authorwith a certain last name. It looks quite a bit more complicated than it is, because I am defining the Models, associating them, syncing with the database (to create their tables), and then creating dummy data in those new tables. Look for the findAllin the middle of the code to see specifically what you're after.

下面是如何将用户Sequelize让所有工作示例BooksAuthor具有一定的姓氏。它看起来比实际复杂得多,因为我正在定义模型,将它们关联起来,与数据库同步(以创建它们的表),然后在这些新表中创建虚拟数据。寻找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')
        });
  }

回答by Adam

In the newest version of Sequilize (5.9.0) the method proposed by @c.hill does not work.

在最新版本的 Sequilize (5.9.0) 中,@c.hill 提出的方法不起作用。

Now you need to do the following:

现在您需要执行以下操作:

return Book.findAll({
    where: {
        '$Authors.lastName$': 'Testerson'
    },
    include: [
        {model: Author, as: Author.tableName}
    ]
});