js sequelize multiple in include in not working

时间:2017-04-28 14:58:31

标签: javascript sequelize.js

我有一个sequelize查询,其中包含多个where模型中的条件:

const scrapingUrl = await db.ScrapingUrl.findOne({
            where: strong text{
                date_downloaded: null,
            },
            include: [ {
                model: db.Action,
                where: {
                    $or: {
                        hourly_scraping_limit: null,
                        scraping_counter: {
                            $or: {
                                $eq: null,
                                $lt: Sequelize.literal('Action.hourly_scraping_limit')
                            }
                        }
                    },
                    $or: {
                        hourly_scraping_limit_per_proxy: null,
                        scraping_counter_per_proxy: {
                            $or: {
                                $eq: null,
                                $lt: Sequelize.literal('Action.hourly_scraping_limit_per_proxy')
                            }
                        }
                    }
                },
                include: [ db.ScrapingContainer ]
            } ]
        });

似乎忽略了hourly_scraping_limit部分,只忽略了hourly_scraping_limit_per_proxy的过滤器。相关的查询部分是:

$or: {
    hourly_scraping_limit: null,
    scraping_counter: {
        $or: {
            $eq: null,
            $lt: Sequelize.literal('Action.hourly_scraping_limit')
        }
    }
},

Sequelize记录以下SQL语句:

SELECT `ScrapingUrl`.`id`, `ScrapingUrl`.`action_id`, `ScrapingUrl`.`url`, `ScrapingUrl`.`date_added`, `ScrapingUrl`.`date_downloaded`, `ScrapingUrl`.`date_scraped`, `ScrapingUrl`.`success_pattern_found`, `ScrapingUrl`.`blocked_pattern_found`, `ScrapingUrl`.`scraping_blob_id`, `Action`.`id` AS `Action.id`, `Action`.`name` AS `Action.name`, `Action`.`scraping_container_id` AS `Action.scraping_container_id`, `Action`.`hourly_scraping_limit` AS `Action.hourly_scraping_limit`, `Action`.`hourly_scraping_limit_per_proxy` AS `Action.hourly_scraping_limit_per_proxy`, `Action`.`scraping_counter` AS `Action.scraping_counter`, `Action`.`scraping_counter_per_proxy` AS `Action.scraping_counter_per_proxy`, `Action`.`scraping_counter_last_reset` AS `Action.scraping_counter_last_reset`, `Action`.`scraping_success_pattern` AS `Action.scraping_success_pattern`, `Action`.`scraping_blocked_pattern` AS `Action.scraping_blocked_pattern`, `Action.ScrapingContainer`.`id` AS `Action.ScrapingContainer.id`, `Action.ScrapingContainer`.`name` AS `Action.ScrapingContainer.name` FROM `scraping_urls` AS `ScrapingUrl` INNER JOIN `actions` AS `Action` ON `ScrapingUrl`.`action_id` = `Action`.`id` AND ((`Action`.`hourly_scraping_limit_per_proxy` IS NULL OR (`Action`.`scraping_counter_per_proxy` IS NULL OR `Action`.`scraping_counter_per_proxy` < Action.hourly_scraping_limit_per_proxy))) LEFT OUTER JOIN `scraping_containers` AS `Action.ScrapingContainer` ON `Action`.`scraping_container_id` = `Action.ScrapingContainer`.`id` WHERE `ScrapingUrl`.`date_downloaded` IS NULL LIMIT 1;

1 个答案:

答案 0 :(得分:1)

$或未正确形成。我认为这就是问题所在。例如:

where: {
  '$or': [
    {bla: true},
    {bla: false}
  ]
}

转换为:

WHERE bla = true OR bla = false

如果您需要嵌套$或:

where: {
  '$or': [
    {'$or': [{foo: true, bar: false}, {foo: false, bar: true}]},
    {'$or': [{bla: true, ble: false}, {bla: false, ble: true}]},
  ]
}

转换为:

WHERE ((foo = true AND bar = false) OR (foo = false AND bar = true)) OR ((foo = false AND bar = true) OR (foo = true AND bar = false))