Sequelize query - compare dates in two columns

1,844 views
Skip to first unread message

CJ AP

unread,
May 26, 2015, 7:53:23 AM5/26/15
to sequ...@googlegroups.com

I have a table in Sequelize with two date columns - i.e. with :

var Visit = sequelize.define("Visit", {
  /*...*/
  scheduleEndDate: {
    type: DataTypes.DATE
  }
  actualEndDate: {
    type: DataTypes.DATE
  }
  /*...*/
});

I want to make a query that returns rows where actualEndDate is before scheduleEndDate - and can't get the format right. What I've tried for the where part of my findAll query is:

where: { actualEndDate: { lt: Visit.scheduleEndDate } }

- throws an error because Visit not defined (have also tried with this.scheduleEndDate and scheduleEndDate - also throw an error)

where: { actualEndDate: '< scheduleEndDate' }

- does a string comparison of actualEndDate against the string '< scheduleEndDate'

Do I need to define an instance method to do the date comparison / how best to do this query?

Mick Hansen

unread,
May 26, 2015, 7:57:30 AM5/26/15
to CJ AP, sequ...@googlegroups.com
Sequelize doesn't support this in a great way right now. Please open a feature request on github for samle-table column comparisons and we can discuss syntax.

In the short term you'll have to do a raw comparison with ['actualEndDate < scheduleEndDate']
--
Mick Hansen
@mhansendev
mhansen.io

CJ AP

unread,
May 26, 2015, 8:02:01 AM5/26/15
to sequ...@googlegroups.com, charle...@agileprojects.com
Thanks for the quick response Mick. I was hoping to avoid raw query - and assumed I'd missed something in the spec.

Charles.
Reply all
Reply to author
Forward
0 new messages