-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
node-mssql
v6 incorrectly stores Dates
#3202
node-mssql
v6 incorrectly stores Dates
#3202
Comments
With some experimenting, 1 second after midnight will write a date of November 30th, but 1 MINUTE after midnight, will write the date of December 1st:
writes:
and:
writes:
You can see that date jumps from November 30th to December 1st at the 38 seconds after midnight mark:
|
(sorry for all the comments) You can download my test project here if you would like to try yourself without setting up a new project. |
After #3162 this is the only remaining issue blocking unit tests from passing on SQL Server. |
I'm not sure about the exact issue you are running into but are you aware that mysql (driver) will attempt to do timezone conversion automatically. Perhaps this is contributing to the confusion. There is a |
As I remember you should have your timezone to set to UTC to prevent all different issues. Adding @chriskalmar @pburrows can you please review #1717 and all related issues? |
I looked at those issues and they don't seem to fix the problem. I can definitely reproduce it every time with the latest code using the project I linked. It happens with I am trying to narrow down where the problem is. I've narrowed it down so far to be somewhere in the SqlServerQueryRunner.ts, in the I'll post again when I can narrow it down. |
@pburrows did you set a local timezone to UTC? |
(oops! didn't mean to hit close!) |
I tried setting process.env.TZ, but it didn't really make a difference. I did not try changing the timezone on my OS, cause that does not seem like a reasonable solution. Heh. |
just try to, just to see if it works =) |
Heading for vacation in a bit, but I'll give it a shot when I get back. My guess is that I will have the opposite problem of trying to make a non-UTC date to reproduce the issue with (because dates, by default, use the host's timezone, so they will be UTC, so nothing will need to be translated). So it would be hard to reproduce this bug (or even notice it!) if your OS date happens to be UTC. |
Also worth mentioning, this is only happening with |
Here it's happening with |
same here. Change TypeORM entity definition for date column to "DateTime" works. Table definition in database can still be "datetime2(7)". Also |
I'm also having this issue: I solved by changing the entity column form datetime2 to datetime but I don't understand why input date is converted again if |
Tests for mssql are now passing in CI. Any time we use the older mssql module it acts up like this. Might be a good place to look for debugging. We've confirmed it as working in our CI environment and I'll be closing this out. |
Can you clarify "older mssql module?" Also, I do not see a specific unit test for this issue (#3202) in the issue specific list of typeorm unit tests. If this is covered by a different specific issue, let me know. |
I was mistaken and the fix in v6 was about A commenter in another issue had mentioned that v5 was having this issue but v6 was not. Digging into this a bit, though, to confirm the issue, it seems that the comment was in reference to Reopening and doing a bit of research :) |
We have installed the versions for testing:
We do have tests that verify the various column types work for an identity check. Howeer, looks like some of them had been commented out - didn't see that before. Bummer, because that's what I was referring to for tests. I created a test specifically for this issue. Ok - so the issue is in Let's look at the client: It looks like we're binding the right date to the MSSQL client & doing everything as expected for the query but it fails when the server constructs it all somehow, and passes over the wire. If I change the type we're binding it as - manually forcing all To take TypeORM out of the equation, I've set up a minimal reproduction
This ALSO exhibits the issue which means that this is not a bug in TypeORM but in Digging further, the
If you replace
You get the following outputs:
This is apparently where this bug is - this is how we end up with the wrong date. The rounding causes the date to be off by one. Thing is.. this code was changed by the latest release of tedious. Experimenting with the next release means updating to mssql 7.0.0-alpha.1 - which I did for the test. A few small changes needed but otherwise easy to get working. And in testing - this fixes the problem. I think that can confirm that this is a bug in Tedious. The PR that fixed it in tedious is tediousjs/tedious#1023 Problem is, though - we cannot install that alpha release & use it because it's not compatible with the version we're using now. Until it's out of alpha I'm not comfortable coding against it, either. @pburrows Does that help at all? |
node-mssql
v6 incorrectly stores Dates
Thanks, so much, Jim! Great analysis. I am very happy you discovered the issue. That's awesome. I guess now it is just a matter of waiting until everything gets released. |
Hi! I'm trying to test this solution because we have a similar issue with
Throws this error: Any ideas? |
This suggests you have to use an enum or something tediousjs/node-mssql#482 |
I've tried using ISOLATION_LEVEL enum, but typeorm SqlConnectionOptions only validates "strings" |
You can use the enum with |
Previous error dissapear, but now it shows:
when I try to do a DB operation (the login SELECT in this case) |
I have the same problem with datetime type. When I save for example 2020-11-24T00: 00: 00.000Z in the database it stores 2020-11-23T00: 00: 00.000Z (one day less). |
This upgrades the `mssql` driver to version 7 to resolve #3202. This issue was created in the bulk insert test adding a datetime2 field with a time of midnight. v7 no longer allows numeric parameter names and was throwing an error inside tedious but you can see that `node-mssql` is expecting a string. https://github.com/tediousjs/node-mssql/blob/76585f973dd8cad48836fe302f99f67d47ff6129/lib/base/request.js#L101 Another user saw this issue in the comments of issue #3202. #3202 (comment) Stringifying the numeric parameter names before it enters the driver resolves this issue. Now all mssql tests pass locally once you provide additional config in `ormconfig.json` locally required for a locally running docker instance of mssql which uses a self signed certificate. ```json "extra": { "trustServerCertificate": true } ```
This upgrades the `mssql` driver to version 7 to resolve typeorm#3202. This issue was created in the bulk insert test adding a datetime2 field with a time of midnight. v7 no longer allows numeric parameter names and was throwing an error inside tedious but you can see that `node-mssql` is expecting a string. https://github.com/tediousjs/node-mssql/blob/76585f973dd8cad48836fe302f99f67d47ff6129/lib/base/request.js#L101 Another user saw this issue in the comments of issue typeorm#3202. typeorm#3202 (comment) Stringifying the numeric parameter names before it enters the driver resolves this issue. Now all mssql tests pass locally once you provide additional config in `ormconfig.json` locally required for a locally running docker instance of mssql which uses a self signed certificate. ```json "extra": { "trustServerCertificate": true } ``` (cherry picked from commit 4711a71)
This upgrades the `mssql` driver to version 7 to resolve typeorm#3202. This issue was created in the bulk insert test adding a datetime2 field with a time of midnight. v7 no longer allows numeric parameter names and was throwing an error inside tedious but you can see that `node-mssql` is expecting a string. https://github.com/tediousjs/node-mssql/blob/76585f973dd8cad48836fe302f99f67d47ff6129/lib/base/request.js#L101 Another user saw this issue in the comments of issue typeorm#3202. typeorm#3202 (comment) Stringifying the numeric parameter names before it enters the driver resolves this issue. Now all mssql tests pass locally once you provide additional config in `ormconfig.json` locally required for a locally running docker instance of mssql which uses a self signed certificate. ```json "extra": { "trustServerCertificate": true } ```
Issue type:
[x ] question
[x] bug report
Database system/driver:
[x]
mssql
TypeORM version:
[x]
latest
Steps to reproduce or a small repository showing the problem:
I am trying to write a date, devoid of timezone information (a UTC date), to my SQL Server database. I created a test project to try it out.
(NOTE: I am in the eastern timezone)
Here are some of the ways I am trying to write the date '12/1/2018' to the database:
The most basic:
writes the following record to the database:
This date is off by 24 hours!!!!
The SQL generated by TypeOrm is this:
Which, if you put the parameters into the query you get this sql statement:
If you run THAT SQL Statement in SQL Server Management Studio (by copy and pasting), it writes this record to the database:
Which isn't exactly what I am looking for, but is wrong in a way I understand (it added my local timezone to the date string it generated) and that I can account for.
Why is the data written to the database different when the SQL Statement is run manually vs run by TypeORM?
How do I correctly write a UTC date to the database?
The text was updated successfully, but these errors were encountered: