首页 文章

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

提问于
浏览
55

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

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);
})

我想要一个回复一个帖子的查询,其中包含用户的信息 . 在原始查询中,我得到这个:

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

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

3 回答

  • 70
    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`;
    

    上面的查询与您发布的内容相比可能看起来有点复杂,但它的作用基本上只是对users表的所有列进行别名,以确保在返回时将它们放入正确的模型中,而不是与posts模型混淆

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

    进一步阅读:

  • 39

    虽然接受的答案在技术上并不是错误的,但它没有回答原始问题,也没有回答评论中的后续问题,而这正是我来到这里寻找的 . 但我明白了,所以这里 .

    如果要查找具有用户(并且只有具有用户的用户)的所有帖子,其中SQL将如下所示:

    SELECT * FROM posts INNER JOIN users ON posts.user_id = users.id
    

    这在语义上与OP的原始SQL相同:

    SELECT * FROM posts, users WHERE posts.user_id = users.id
    

    那就是你想要的:

    Posts.findAll({
      include: [{
        model: User,
        required: true
       }]
    }).then(posts => {
      /* ... */
    });
    

    设置为true是设置内部联接的关键 . 如果你想要一个左外连接(你获得所有帖子,无论是否有用户链接),然后将所需更改为false,或将其保持关闭,因为这是默认值:

    Posts.findAll({
      include: [{
        model: User,
    //  required: false
       }]
    }).then(posts => {
      /* ... */
    });
    

    如果要查找属于出生年份为1984年的用户的所有帖子,您需要:

    Posts.findAll({
      include: [{
        model: User,
        where: {year_birth: 1984}
       }]
    }).then(posts => {
      /* ... */
    });
    

    请注意,只要在其中添加where子句,默认情况下为true .

    如果您想要所有帖子,无论是否有用户附加但是如果有用户,那么只有1984年出生的用户,那么请将所需的字段添加回来:

    Posts.findAll({
      include: [{
        model: User,
        where: {year_birth: 1984}
        required: false,
       }]
    }).then(posts => {
      /* ... */
    });
    

    如果你想要名称为“Sunshine”的所有帖子,并且只有它属于1984年出生的用户,你可以这样做:

    Posts.findAll({
      where: {name: "Sunshine"},
      include: [{
        model: User,
        where: {year_birth: 1984}
       }]
    }).then(posts => {
      /* ... */
    });
    

    如果您想要名称为“Sunshine”的所有帖子,并且只有当它属于与帖子上的post_year属性匹配的同一年出生的用户时,您才会这样做:

    Posts.findAll({
      where: {name: "Sunshine"},
      include: [{
        model: User,
        where: ["year_birth = post_year"]
       }]
    }).then(posts => {
      /* ... */
    });
    

    我知道,有人会在他们出生的那一年发表一篇文章是没有意义的,但这只是一个例子 - 顺其自然 . :)

    我(主要)从这个文档中找到了这个:

  • 3
    Model1.belongsTo(Model2, { as: 'alias' })
    
    Model1.findAll({include: [{model: Model2  , as: 'alias'  }]},{raw: true}).success(onSuccess).error(onError);
    

相关问题