0

I have a sequelize query with multiple where conditions in an include model:

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

It seems to ignore hourly_scraping_limit part and only filters for hourly_scraping_limit_per_proxy. The relevant query part is this:

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

Sequelize logs the following SQL statement:

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 Answer 1

1

The $or is not properly formed. I think that's the issue. Example:

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

Translates to:

WHERE bla = true OR bla = false

If you need to nest $or:

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

Translates to:

WHERE ((foo = true AND bar = false) OR (foo = false AND bar = true)) OR ((foo = false AND bar = true) OR (foo = true AND bar = false))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.