node.js - 仅当 active = true 时 Sequelize ORM 包含模型

标签 node.js postgresql orm sequelize.js

我如何才能找到 ID 为 1 的所有订单并仅在该商品具有 active = true 时才包含该商品?否则会出现空数组...

Order.findAll({
    where: { id: 1 },
    include: [
      { model: Item, where: sequelize.and({'active' : true }) }
    ]
}).then(function(order) {
    callback(null, order);
});

这只显示订单,其中一些商品的 active = true。我想显示所有带有 id: 1 的订单和作为子数组的项目...

最佳答案

来自 Sequelize Tutorial :

Adding a where clause to the include automatically makes it required

解决方案是这样的:

Order.findAll({
    where: { id: 1 },
    include: [
      { model: Item, 
        where: sequelize.and({'active' : true }),
        required: false 
      }
    ]
}).then(function(order) {
    callback(null, order);
});

关于node.js - 仅当 active = true 时 Sequelize ORM 包含模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30953949/

相关文章:

javascript - npm 启动失败

node.js - nodeJS proxyquire 覆盖要求的特定功能

javascript - 是否可以让交互式 Node.js shell 使用 vi 模式?

ruby-on-rails - 无法阻止 Rails 中的 SQL 注入(inject)查询

android - ActiveAndroid 数据库位置动态变化

c# - EF : using typeid map entity framework hierarchy

mysql - 在 Dockerfile 中创建动态用户 - MySQL

postgresql - Postgres COPY FROM csv 文件 - 没有这样的文件或目录

Django "MigrationSchemaMissing: Unable to create the django_migrations table (no schema has been selected to create in)"

mysql - 简单的 hibernate 不起作用,但级联查询可以