简体   繁体   English

如何使用 Sequelize 连接两个表?

[英]How do I join two tables using Sequelize?

I have 3 tables: Books Authors and AuthorBooks.我有 3 个表:Books Authors 和 AuthorBooks。 The last one to solve many-to-many.最后一个解决多对多。 So far i managed to get the tables to work and display as i expected to.到目前为止,我设法让表格按预期工作和显示。 Now i want to display a table that shows data from both tables: Books and Authors.现在我想显示一个表格,显示两个表格中的数据:书籍和作者。 BooksAuthors stores only the IDs of the other tables. BooksAuthors 仅存储其他表的 ID。 I want something like this to work with sequelize:我想要这样的东西与续集一起工作:

SELECT ab.idA, ab.idB, a.firstName, a.lastName, b.title, b.description
from authorbooks ab
left join authors a on ab.idA=a.idA
left join books b on ab.idB=b.idB;

This is how i tried to define the relationship.这就是我试图定义这种关系的方式。 I think I'm missing something here.我想我在这里遗漏了一些东西。

db.models.Books = require("../models/books")(sequelize);
  db.models.Authors = require("../models/authors")(sequelize);
  db.models.AuthorBooks = require("../models/authorbooks")(sequelize);


db.models.Authors.belongsToMany(db.models.Books,{
    through: db.models.AuthorBooks,
    foreignKey:{ name: 'idA',
    allowNull:true
  }
  })
  db.models.Books.belongsToMany(db.models.Authors,{
    through: db.models.AuthorBooks,
    foreignKey:{ name: 'idB',
    allowNull: true
  }
  });

  module.exports = db;

This is how i defined the tables.这就是我定义表格的方式。 BOOKS:图书:

    module.exports = (sequelize) => {

  class Books extends Sequelize.Model {}


  Books = sequelize.define('Books', {
    idB:{
      type: Sequelize.INTEGER,
      allowNull: false,
      primaryKey : true

  },
  title: {
      type: Sequelize.STRING
  },
  description:{ 
      type: Sequelize.STRING(6000)
  },
  });


  return Books;
};

AUTHORS:作者:

         module.exports = (sequelize) => {

      class Authors extends Sequelize.Model {}


      Authors = sequelize.define('Authors', {
        idA:{
          type: Sequelize.INTEGER,
          allowNull: false,
          primaryKey : true

      },
      firstName: {
          type: Sequelize.STRING
      },
      lastName:{ 
          type: Sequelize.STRING
      },
      });


      return Authors;
  };

AUTHORBOOKS:作者书籍:

module.exports = (sequelize) => {


    class AuthorBooks extends Sequelize.Model {}

    AuthorBooks.init({

      id: {
        type: Sequelize.INTEGER,
        primaryKey:true,
        autoIncrement: true
      }

    },{sequelize});


  return AuthorBooks;
}

I think you can use the "include" option to achieve this function.我认为您可以使用“包含”选项来实现此 function。 For example, you can use the code like例如,您可以使用如下代码

Book.findAll({
include:[{model:Author}]
})

This option will add an "Author" field including the author information to the result.此选项将在结果中添加一个包含作者信息的“作者”字段。

What you need is a through options in the query, it combines all the authors linked to a particular book, empty attributes will avoid adding any attribute from mapping table which in your case is 'AuthorBooks'.您需要的是查询中的一个通过选项,它结合了链接到特定书籍的所有作者,空属性将避免从映射表中添加任何属性,在您的情况下是“AuthorBooks”。

Book.findAll({
    include:[{
        model:Author,
        through: {
            attributes: []
           }
    }]
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM