node.js - 如何在 ON 子句中使用多个谓词在 Sequelize 中执行 LEFT JOIN?

标签 node.js postgresql orm sequelize.js

这是我非常简单的 Sequelize 模型关系:

    models["Post"]
        .hasMany(models["PostLike"])
    models["PostLike"]
        .belongsTo(models["Post"])

这是我的 Sequelize.findAll 查询(用 CoffeeScript 编写):

Post.findAll
            include : [ PostLike ]
            where : where
            offset : start
            limit : limit
            order : order
.success (posts) =>
     ......
.failure (error) =>
     ......

如您所见,我包含了 PostLike 模型并且 Sequelize 生成了正确的 LEFT JOIN:

...FROM "posts" LEFT JOIN "post_likes" AS "post_likes" 
    ON "posts"."id" = "posts_likes"."post_id" ...

但是,我想让 Sequelize 使用我的自定义条件扩展 ON 谓词:

... ON "posts"."id" = "posts_likes"."post_id" AND posts_likes.author_id = 123

这可能很容易做到,我只是在文档中找不到它。

谢谢

最佳答案

请原谅缺少 CoffeeScript,但您可以这样做:

Post.findAll({
    include: [{
        model: PostLike,
        where: { author_id: 123 }
    }]
})

我在代码中发现了以下可能有用的注释。

* @param  {Array<Object|Model>}       [options.include] A list of associations to eagerly load using a left join. Supported is either `{ include: [ Model1, Model2, ...]}` or `{ include: [{ model: Model1, as: 'Alias' }]}`. If your association are set up with an `as` (eg. `X.hasMany(Y, { as: 'Z }`, you need to specify Z in the as attribute when eager loading Y).
   * @param  {Model}                     [options.include[].model] The model you want to eagerly load
   * @param  {String}                    [options.include[].as] The alias of the relation, in case the model you want to eagerly load is aliassed. For `hasOne` / `belongsTo`, this should be the singular name, and for `hasMany`, it should be the plural
   * @param  {Association}               [options.include[].association] The association you want to eagerly load. (This can be used instead of providing a model/as pair)
   * @param  {Object}                    [options.include[].where] Where clauses to apply to the child models. Note that this converts the eager load to an inner join, unless you explicitly set `required: false`
   * @param  {Array<String>}             [options.include[].attributes] A list of attributes to select from the child model
   * @param  {Boolean}                   [options.include[].required] If true, converts to an inner join, which means that the parent model will only be loaded if it has any matching children. True if `include.where` is set, false otherwise.
   * @param  {Array<Object|Model>}       [options.include[].include] Load further nested related models

关于node.js - 如何在 ON 子句中使用多个谓词在 Sequelize 中执行 LEFT JOIN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26929911/

相关文章:

javascript - 当我将 Node.js https 服务器分成两个文件时,它无法工作

php - PhalconPHP : eager load related records?

java - 使用 Hibernate 连接到 SQL Server

javascript - 对数据库和服务器使用 Promise

javascript - Node JS - HTML 路径

postgresql - 如何在复合类型的 Postgres 数组中存储双引号?

postgresql - 使用 Lisp/Postmodern/PostgreSQL 变量作为表名

java - Hibernate、SQL 和递归关联

node.js - 反向代理和Socket.io,websocket握手失败

postgresql - 从 10 亿行 GreenPlum DB 中缓慢选择