You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Raw queries convert Date replacements to local timezone. #10493
changed the title [-]Raw queries converts Date replacements to local timezone.[/-][+]Raw queries convert Date replacements to local timezone.[/+]on Mar 2, 2019
I'm also running into this problem. The debug logging of the query shows that my input is converted to local time even though it really should be UTC. All the data in the table is UTC as well.
SELECT labels.id AS id, COUNT(cards.id) AS total_reviews
FROM labels
LEFT JOIN card_label ON labels.id = card_label.label_id
LEFT JOIN cards ON cards.id = card_label.card_id
AND cards.next_reminder_at <= '2019-04-25 21:34:54.501 -07:00'
WHERE labels.user_id = '12345'
GROUP BY labels.id;
Same here, it cannot be possible that a library does not behave as you expect. With PostgreSQL there's no problem with date but why there should be a different date behaviour with MySQL?
That makes no sense, and moreover why there's should be an automatic casting to timezone with MySQL but not with PostgreSQL? It should be a connection option and no casting to timezone at default.
This dirty fix based on above solution by @danieldkim solve the issue acting on a one-level data Object named row with some Date fields.
Must be solved in core though.
If you work with timezones, the best way is to store dates in UTC and get them from DB with setting your timezone.
For correct using queries with timezone, prepare your pg driver, full answer see StackOverflow
Suppose we make a SQL query, select all User's fields, and "createdAt" in timezone 'Europe/Kiev':
SELECT"createdAt"::timestamptz AT TIME ZONE 'Europe/Kiev'FROM users WHERE id =1;
# or with variablesSELECT"createdAt"::timestamptz AT TIME ZONE :timezone FROM users WHERE id = :id;
For Sequelize (for User model) it will be something like this:
sequelize.findOne({where: {id: 1},attributes: {include: [[sequelize.literal(`"User"."createdAt"::timestamptz AT TIME ZONE 'Europe/Kiev'`),'createdAt'],// also you can use variables, of course remember about SQL injection:// [sequelize.literal(`"User"."updatedAt"::timestamptz AT TIME ZONE ${timeZoneVariable}`), 'updatedAt'],]}});
So with undefined, Date replacements get stringified in the host's local zone, and options.timezone (default "+00:00") is ignored for raw queries only. Model operations are unaffected, which is what makes it confusing to diagnose.
The instant itself is correct; it's just rendered in the wrong frame, and it's then compared against a column stored in UTC. On UTC hosts local == UTC so nothing is visibly wrong, which is why this tends to surface only on developer machines — in our case as a date-range filter silently dropping every row from the last 4 hours.
Both replacement styles are affected (named :param at line 133, positional ? at line 153).
Workaround for anyone landing here: format the value to a string before passing it, so there is no Date left for Sequelize to convert.
Two-line fix, if a v6 patch would be considered — the dialect already holds the instance (this.sequelize is set in its constructor), so the configured value is reachable:
We are running this as a yarn patch locally and it resolves the issue with no other behavioural change (on UTC hosts the emitted SQL is byte-identical).
Happy to open a PR if that is useful — though I appreciate v6 is in maintenance, and I have not verified whether v7 is affected, so this may well already be resolved there by the data types rewrite in #14505.
Activity
[-]Raw queries converts Date replacements to local timezone.[/-][+]Raw queries convert Date replacements to local timezone.[/+]5amfung commentedon Apr 26, 2019
I'm also running into this problem. The debug logging of the query shows that my input is converted to local time even though it really should be UTC. All the data in the table is UTC as well.
danieldkim commentedon May 22, 2019
workaround in the meantime, assuming you've JSON-stringified a
Dateobject into adateJsonvariable:german970814 commentedon Sep 27, 2019
Any updates about it?
xCloudx8 commentedon Oct 3, 2019
Same here, it cannot be possible that a library does not behave as you expect. With PostgreSQL there's no problem with date but why there should be a different date behaviour with MySQL?
That makes no sense, and moreover why there's should be an automatic casting to timezone with MySQL but not with PostgreSQL? It should be a connection option and no casting to timezone at default.
papb commentedon Oct 30, 2019
@xCloudx8 I agree, I've seen a few timezone issues, hopefully they will be sorted out soon :)
wondie commentedon Oct 10, 2020
I have to abandon sequelize because of time zone issue.
chirale commentedon Apr 1, 2021
This dirty fix based on above solution by @danieldkim solve the issue acting on a one-level data Object named row with some Date fields.
Must be solved in core though.
igolskyi commentedon Dec 7, 2021
If you work with timezones, the best way is to store dates in UTC and get them from DB with setting your timezone.
For correct using queries with timezone, prepare your pg driver, full answer see StackOverflow
Suppose we make a SQL query, select all User's fields, and "createdAt" in timezone 'Europe/Kiev':
For Sequelize (for User model) it will be something like this:
mainfraame commentedon Jul 19, 2026
Still reproducible on 6.37.8 (current latest v6). Adding the exact call site, since it took a while to track down and this thread doesn't name it.
The root cause is that
injectReplacementsnever receives the connection'stimezoneand passesundefinedtoescape():escape()does accept and honour a timezone — it forwards it straight to the data type:So with
undefined,Datereplacements get stringified in the host's local zone, andoptions.timezone(default"+00:00") is ignored for raw queries only. Model operations are unaffected, which is what makes it confusing to diagnose.Concretely, with
TZ=America/New_York:The instant itself is correct; it's just rendered in the wrong frame, and it's then compared against a column stored in UTC. On UTC hosts local == UTC so nothing is visibly wrong, which is why this tends to surface only on developer machines — in our case as a date-range filter silently dropping every row from the last 4 hours.
Both replacement styles are affected (named
:paramat line 133, positional?at line 153).Workaround for anyone landing here: format the value to a string before passing it, so there is no
Dateleft for Sequelize to convert.Two-line fix, if a v6 patch would be considered — the dialect already holds the instance (
this.sequelizeis set in its constructor), so the configured value is reachable:We are running this as a
yarn patchlocally and it resolves the issue with no other behavioural change (on UTC hosts the emitted SQL is byte-identical).Happy to open a PR if that is useful — though I appreciate v6 is in maintenance, and I have not verified whether v7 is affected, so this may well already be resolved there by the data types rewrite in #14505.