• <legend id='ThYBA'><style id='ThYBA'><dir id='ThYBA'><q id='ThYBA'></q></dir></style></legend>

      <i id='ThYBA'><tr id='ThYBA'><dt id='ThYBA'><q id='ThYBA'><span id='ThYBA'><b id='ThYBA'><form id='ThYBA'><ins id='ThYBA'></ins><ul id='ThYBA'></ul><sub id='ThYBA'></sub></form><legend id='ThYBA'></legend><bdo id='ThYBA'><pre id='ThYBA'><center id='ThYBA'></center></pre></bdo></b><th id='ThYBA'></th></span></q></dt></tr></i><div id='ThYBA'><tfoot id='ThYBA'></tfoot><dl id='ThYBA'><fieldset id='ThYBA'></fieldset></dl></div>

      <small id='ThYBA'></small><noframes id='ThYBA'>

        <bdo id='ThYBA'></bdo><ul id='ThYBA'></ul>
    1. <tfoot id='ThYBA'></tfoot>

        如何在 Node.js 上使用 Sequelize 进行连接查询

        How to make join queries using Sequelize on Node.js(如何在 Node.js 上使用 Sequelize 进行连接查询)
        • <tfoot id='PgPK3'></tfoot>
            <tbody id='PgPK3'></tbody>
          <i id='PgPK3'><tr id='PgPK3'><dt id='PgPK3'><q id='PgPK3'><span id='PgPK3'><b id='PgPK3'><form id='PgPK3'><ins id='PgPK3'></ins><ul id='PgPK3'></ul><sub id='PgPK3'></sub></form><legend id='PgPK3'></legend><bdo id='PgPK3'><pre id='PgPK3'><center id='PgPK3'></center></pre></bdo></b><th id='PgPK3'></th></span></q></dt></tr></i><div id='PgPK3'><tfoot id='PgPK3'></tfoot><dl id='PgPK3'><fieldset id='PgPK3'></fieldset></dl></div>

              <legend id='PgPK3'><style id='PgPK3'><dir id='PgPK3'><q id='PgPK3'></q></dir></style></legend>

                <small id='PgPK3'></small><noframes id='PgPK3'>

                  <bdo id='PgPK3'></bdo><ul id='PgPK3'></ul>
                • 本文介绍了如何在 Node.js 上使用 Sequelize 进行连接查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 sequelize ORM;一切都很棒而且很干净,但是当我将它与 join 查询一起使用时遇到了问题.我有两个模型:用户和帖子.

                  I am using sequelize ORM; everything is great and clean, but I had a problem when I use it with join queries. I have two models: users and posts.

                  var User = db.seq.define('User',{
                      username: { type: db.Sequelize.STRING},
                      email: { type: db.Sequelize.STRING},
                      password: { type: db.Sequelize.STRING},
                      sex : { type: db.Sequelize.INTEGER},
                      day_birth: { type: db.Sequelize.INTEGER},
                      month_birth: { type: db.Sequelize.INTEGER},
                      year_birth: { type: db.Sequelize.INTEGER}
                  
                  });
                  
                  User.sync().success(function(){
                      console.log("table created")
                  }).error(function(error){
                      console.log(err);
                  })
                  
                  
                  var Post = db.seq.define("Post",{
                      body: { type: db.Sequelize.TEXT },
                      user_id: { type: db.Sequelize.INTEGER},
                      likes: { type: db.Sequelize.INTEGER, defaultValue: 0 },
                  
                  });
                  
                  Post.sync().success(function(){
                      console.log("table created")
                  }).error(function(error){
                      console.log(err);
                  })
                  

                  我想要一个查询,该查询以发布用户信息的帖子作为响应.在原始查询中,我得到了这个:

                  I want a query that respond with a post with the info of user that made it. In the raw query, I get this:

                  db.seq.query('SELECT * FROM posts, users WHERE posts.user_id = users.id ').success(function(rows){
                              res.json(rows);
                          });
                  

                  我的问题是如何更改代码以使用 ORM 样式而不是 SQL 查询?

                  My question is how can I change the code to use the ORM style instead of the SQL query?

                  推荐答案

                  User.hasMany(Post, {foreignKey: 'user_id'})
                  Post.belongsTo(User, {foreignKey: 'user_id'})
                  
                  Post.find({ where: { ...}, include: [User]})
                  

                  哪个会给你

                  SELECT
                    `posts`.*,
                    `users`.`username` AS `users.username`, `users`.`email` AS `users.email`,
                    `users`.`password` AS `users.password`, `users`.`sex` AS `users.sex`,
                    `users`.`day_birth` AS `users.day_birth`,
                    `users`.`month_birth` AS `users.month_birth`,
                    `users`.`year_birth` AS `users.year_birth`, `users`.`id` AS `users.id`,
                    `users`.`createdAt` AS `users.createdAt`,
                    `users`.`updatedAt` AS `users.updatedAt`
                  FROM `posts`
                    LEFT OUTER JOIN `users` AS `users` ON `users`.`id` = `posts`.`user_id`;
                  

                  与您发布的内容相比,上面的查询可能看起来有点复杂,但它所做的基本上只是为用户表的所有列设置别名,以确保在返回时将它们放入正确的模型中,而不是与帖子混淆型号

                  The query above might look a bit complicated compared to what you posted, but what it does is basically just aliasing all columns of the users table to make sure they are placed into the correct model when returned and not mixed up with the posts model

                  除此之外,您会注意到它执行 JOIN 而不是从两个表中进行选择,但结果应该是相同的

                  Other than that you'll notice that it does a JOIN instead of selecting from two tables, but the result should be the same

                  进一步阅读:

                  • http://docs.sequelizejs.com/en/latest/docs/associations/#one-to-one-associations
                  • http://docs.sequelizejs.com/en/latest/docs/associations/#one-to-many-associations
                  • http://docs.sequelizejs.com/en/latest/docs/models-usage/#eager-loading

                  这篇关于如何在 Node.js 上使用 Sequelize 进行连接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Can#39;t Create Entity Data Model - using MySql and EF6(无法创建实体数据模型 - 使用 MySql 和 EF6)
                  MySQL select with CONCAT condition(MySQL选择与CONCAT条件)
                  Capitalize first letter of each word, in existing table(将现有表格中每个单词的首字母大写)
                  How to retrieve SQL result column value using column name in Python?(如何在 Python 中使用列名检索 SQL 结果列值?)
                  Update row with data from another row in the same table(使用同一表中另一行的数据更新行)
                  Exporting results of a Mysql query to excel?(将 Mysql 查询的结果导出到 excel?)

                      <tfoot id='9Zips'></tfoot>
                    • <small id='9Zips'></small><noframes id='9Zips'>

                        <bdo id='9Zips'></bdo><ul id='9Zips'></ul>

                        <i id='9Zips'><tr id='9Zips'><dt id='9Zips'><q id='9Zips'><span id='9Zips'><b id='9Zips'><form id='9Zips'><ins id='9Zips'></ins><ul id='9Zips'></ul><sub id='9Zips'></sub></form><legend id='9Zips'></legend><bdo id='9Zips'><pre id='9Zips'><center id='9Zips'></center></pre></bdo></b><th id='9Zips'></th></span></q></dt></tr></i><div id='9Zips'><tfoot id='9Zips'></tfoot><dl id='9Zips'><fieldset id='9Zips'></fieldset></dl></div>

                          <tbody id='9Zips'></tbody>
                            <legend id='9Zips'><style id='9Zips'><dir id='9Zips'><q id='9Zips'></q></dir></style></legend>