question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

findAndCountAll is not working with DATE_SUB function

See original GitHub issue

Issue Description

What are I doing?

I’m trying to filter some orders by their states and updated_at time.

   const {
       Op,
       fn: dbFunc,
       literal: dbLiteral
    } = require('sequelize')
   
    const paginator = {
        DEFAULT_LIMIT_PER_PAGE,
        calculateOffset: (page, limit = DEFAULT_LIMIT_PER_PAGE) => {
            return (page - 1) * limit
        },
       calculateTotalPages: (total, limit = DEFAULT_LIMIT_PER_PAGE) => {
            return Math.ceil(total / limit)
        }
     }

    const prepareConds = (prevConditions, key, cond, val) => {
       const opts = {
          ...prevConditions
      }
       if (!opts.where) {
           opts.where = {}
       }
       if (!opts.where[key]) {
           opts.where[key] = {}
       }
       opts.where[key][cond] = val
       return opts
   }

    async function find ({
      id = null,
      o_state: oState = null,
      some_state: someState = null,
      vip_event_id: vipEventId = null,
      seller_id: sellerId = null,
      consumer_id: consumerId = null,
      elapsed_time: elapsedTime = null,
      page = 1,
      limit = PAGESIZE
    } = {}) {
      limit = limit || PAGESIZE

      const offset = paginator.calculateOffset(page, limit)
      let opts = {
        limit,
        offset,
        logging: console.log
      }

      if (id) {
        opts = prepareConds(opts, 'id', Op.eq, id)
      }

      if (consumerId) {
        opts = prepareConds(opts, 'consumer_id', Op.eq, consumerId)
      }

      if (sellerId) {
        opts = prepareConds(opts, 'seller_id', Op.eq, sellerId)
      }

      if (vipEventId) {
        opts = prepareConds(opts, 'vip_event_id', Op.eq, vipEventId)
      }

      if (oState) {
        opts = prepareConds(opts, 'o_state', Op.like, `%${oState.toUpperCase()}%`)
      }

      someState = someState ? someState.filter(item => item) : []

      if (!oState && someState && Array.isArray(someState) && someState.length) {
        if (someState.length === 1) {
          opts = prepareConds(opts, 'o_state', Op.like, `%${someState[0].toUpperCase()}%`)
        } else {
          opts = prepareConds(opts, 'o_state', Op.in, someState.map(item => item.toUpperCase()))
        }
      }

      if (elapsedTime) {
        opts = prepareConds(opts, 'updated_at', Op.gt, dbFunc(
          'DATE_SUB',
          dbLiteral('NOW()'),
          dbLiteral(`INTERVAL ${elapsedTime} MINUTE`)
        ))
      }

      const result = await orderModel.findAndCountAll(opts)
      const collection = result.rows.map(item => item.toJSON())

      console.log('result ' + util.inspect(result))
      return {
        total: result.count,
        page,
        limit,
        collection
      }
    }

    const currentOrderPage = await find({
      some_state: [
        ORDER_STATE_DRAFT,
        ORDER_STATE_WAITING_PROCESSING,
        ORDER_STATE_PENDING_PAYMENT,
        ORDER_STATE_PROCESSING
      ],
      elapsed_time: 10,
      page: 1,
      limit: 50
    })

    console.log('---> ' + JSON.stringify(currentOrderPage))

What do I expect to happen?

I expect the CurrentOrderPage returns a collection of orders and the total should be the same as the one I obtained when executing the query in mysql cli.

What is actually happening?

The SQL code generated by findAndCountAll is correct, and if I run it through mysql cli I get the correct result, but findAndCountAll returns zero results.

Executing (default): SELECT count(*) AS `count` FROM `orders` AS `orders` WHERE `orders`.`o_state` IN ('DRAFT', 'WAITING_PROCESSING', 'PENDING_PAYMENT', 'PROCESSING') AND `orders`.`updated_at` > DATE_SUB(NOW(), INTERVAL 10 MINUTE);
Executing (default): SELECT `id`, `vip_event_id`, `consumer_id`, `seller_id`, `o_state`, `amount`, `currency`, `first_name`, `last_name`, `du`, `email`, `updated_at`, `created_at` FROM `orders` AS `orders` WHERE `orders`.`o_state` IN ('DRAFT', 'WAITING_PROCESSING', 'PENDING_PAYMENT', 'PROCESSING') AND `orders`.`updated_at` > DATE_SUB(NOW(), INTERVAL 10 MINUTE) LIMIT 0, 50;
result { count: 0, rows: [] }
---> {"total":0,"page":1,"limit":50,"collection":[]}

Environment

  • Sequelize version: 5.21.2
  • Node.js version: 12.13.1
  • Operating System: Ubuntu 18 with kernel 5.0.0-37-generic

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don’t know, I was using mysql, with connector library version mysql2@2.0.2 and database version mysql Ver 14.14 Distrib 5.7.28, for Linux (x86_64)

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don’t know how to start, I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mastepanoskicommented, Jan 16, 2020

Ok, I’m going to provide a sequelize-sscce then.

0reactions
github-actions[bot]commented, Nov 30, 2021

This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the “stale” label. 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequelize - Associate table column value in where condition
You can try to use DATE_SUB() function combined with sequelize.literal . models.Game.findAndCountAll({ where: { status: 'NotStarted', ...
Read more >
pyspark.sql.functions.date_sub - Apache Spark
pyspark.sql.functions.date_sub¶. pyspark.sql.functions. date_sub (start, days)[source]¶. Returns the date that is days days before start.
Read more >
MySQL DATE_SUB() Function - W3Schools
The DATE_SUB() function subtracts a time/date interval from a date and then returns the date. Syntax. DATE_SUB(date, INTERVAL value interval). Parameter Values ...
Read more >
How to use DATE_ADD() and DATE_SUB() to add and ...
When working with customer data in ecommerce it's very common for data ... MySQL provides two useful functions for adding and subtracting date...
Read more >
Toggle a boolean column with sequelize-postgresql
Multi level population in Mongoose is not working ... Sequelize findAndCountAll with substring and distinct with multiple column · adding bitwise and with ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found