Repository navigation
Closed
Labels
dialect: mssqlFor issues and PRs. Things that involve MSSQL (and do not involve all dialects).type: bugDEPRECATED: replace with the "bug" issue type
Milestone
Description
Hi,
I am experiencing an issue with dates.
I use the following declaration for my table
var p_usrnot = sequelize.define("usrnot", {
usrnotincde : {
type : DataTypes.INTEGER,
allowNull : true,
primaryKey : true,
autoIncrement : true
},
usrincde : {
type : DataTypes.INTEGER,
allowNull : false,
defaultValue : "0"
},
subjectdsc : {
type : DataTypes.STRING,
allowNull : true
},
msgdsc : {
type : DataTypes.STRING,
allowNull : true
},
replydsc : {
type : DataTypes.STRING,
allowNull : true
},
notnatincde : {
type : DataTypes.INTEGER,
allowNull : false,
defaultValue : "1"
},
usrnoticon : {
type : DataTypes.STRING,
allowNull : true
},
usrnotcolor : {
type : DataTypes.STRING,
allowNull : true
},
ackpnt:{
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue : true
},
ackreadpnt:{
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue : true
},
rcdreadhrd : {
type : DataTypes.DATE,
allowNull : true
},
rcdreceivehrd : {
type : DataTypes.DATE,
allowNull : true
},
rcdcreatehrd : {
type : DataTypes.DATE,
allowNull : true
},
operctcincde : {
type : DataTypes.INTEGER,
allowNull : false
}
I have configured the timezone property with +01:00
I tried to set defaultValue: sequelize.Sequelize.NOW to my rcdcreatehrd but at each time I try to insert a date, it is setted like this : 2016-02-11 17:54:37.0930000 +00:00 and so when I try to make a new Date of this, I get :Thu Feb 11 2016 18:54:37 GMT+0100 (CET), so my date is one hour later than the real hour
I tried removing the timezone, send new Date().toJSON(), new Date().toUTCString()...
What can I do ?
I am using mssql with DATETIMEOFFSET (but tried DATETIME2 too without success).
Thanks
Metadata
Metadata
Assignees
Labels
dialect: mssqlFor issues and PRs. Things that involve MSSQL (and do not involve all dialects).type: bugDEPRECATED: replace with the "bug" issue type
Activity
janmeier commentedon Feb 11, 2016
2016-02-11 17:54:37.0930000 +00:00andThu Feb 11 2016 18:54:37 GMT+0100 (CET)represent the same point in time.I'm not quite sure what your question is - can you give a more concrete example - some code that tries the insert, the accompanying SQL, and what you think should happen?
ggrimbert commentedon Feb 12, 2016
I have the table as defined in my precedent post, I try to insert a new entry into this table by sequelize. For my dates I use, client side new Date() that gives me : Fri Feb 12 2016 08:33:05 GMT+0100 (CET).
When sequelize inserts with that date, it's transformed to Fri Feb 12 2016 08:33:05 GMT+0000 (CET). So when I recover my date, on client side, using sequelize findAll, the date returned is Fri Feb 12 2016 08:33:05 GMT+0000, but I need to work with that date so, I create a new date object from this, and get Fri Feb 12 2016 09:33:05 GMT+0100, the date is one hour later than the real one.
To recap : client side date is valid and with the timestamp, when put on DB, the timezone disappear, and when I recover my date from DB, client side adds an hour AND the timezone, so the date is one hour later.
On client side, i use new Date()
Here's the sql that sequelize produces : INSERT INTO p_usrnot OUTPUT INSERTED.* VALUES (N'352',N'fdsds',N'sfdf',N'',N'-1',N'taxi',N'red',0,1,N'2016-02-12 08:40:27.911',N'10001062',N'-1');
As said previously, the timezone parameter of sequelize is setted to +01:00, and I tried without setting this parameter, but the same problem is still appearing.
I think that sequelize should write the timezone to DB as the mssql timestamp method do.
janmeier commentedon Feb 12, 2016
The problem is that datetime in mssql does not support specifying the timezone, see https://msdn.microsoft.com/en-us/library/bb677335.aspx
The insert looks correct - what sequelize actually inserts is a date without timezone, but sequelize knows that it is in CET, and should parse it correctly when fetching again.
I just tested this, and it does indeed seem that the mssql dialect does not parse the date with the correct timezone
[-]Insert new dates [/-][+]MSSQL does not respect timezone setting when parsing dates[/+]ggrimbert commentedon Feb 12, 2016
Not sure of understanding where the problem comes from, wherever I don't use datetime2 but datetimeoffset (https://msdn.microsoft.com/en-us/library/bb630289.aspx), this one know the timezone, and is visible when I query my table.
select SYSDATETIMEOFFSET() which returns 2016-02-12 09:23:40.7895747 +01:00.
I guess that there is no support for this type in sequelize. Do you think it could be long to support this ?
janmeier commentedon Feb 12, 2016
You could override the
Date.stringifymethodsequelize/lib/dialects/mssql/data-types.js
Line 102 in 1182a4a
ggrimbert commentedon Feb 12, 2016
I found a solution, I don't use DATETIMEOFFSET but DATETIME2, and I don't specify the timezone to sequelize. By doing this, the date is stored without timezone, and javascript parses the date correctly.
Do you think this method is viable ?
janmeier commentedon Feb 12, 2016
If you are only ever going to have your servers in the same timezone, then yes :)
But in any case, this is definitely a bug that needs fixing
janmeier commentedon Feb 21, 2016
We should change mssql to use datetimeoffset by default when creating tables in 4.0
The parsing issue cannot be solved in sequelize, we need to contact tedious maintainers about that
ggrimbert commentedon Feb 22, 2016
Not sure that this type should be used by default, it could be a problem with tables that uses DATETIME2.
Maybe you can create a new datatype in sequelize, and make this available for all database types, not only MSSQL.
janmeier commentedon Feb 22, 2016
@ggrimbert What should be special about this new datatype?
DATEin sequelize is already supposed to respect the timezone setting, its only mssql which doesn't.Changing to datetimeoffset for tables generated by sequelize wil at least fix the problem going forward, and it will still work with datetime2 (albeit without timezone support)
ggrimbert commentedon Feb 23, 2016
Sorry, I didn't know that's going to work with datetime2.
brunoventura commentedon Dec 7, 2016
After some hours debugging sequelize I've found a solution to problem with timezone:
Any regrets with this solution?
felixfbecker commentedon Dec 7, 2016
It is using private API
brunoventura commentedon Dec 7, 2016
¯\_(ツ)_/¯ I'm using
sequelize": "^3.27.0, there's a public API to achieve this behavior? (I tried to find without success)https://github.com/sequelize/sequelize/blob/v3.27.0/lib/dialects/mssql/connection-manager.js#L30
felixfbecker commentedon Dec 7, 2016
What is the
optionsobject you are using?If you have a fix, just provide a PR
brunoventura commentedon Dec 7, 2016
the options sent in Sequelize constructor:
The only thing to do here is expose this method to public API, but in v4.0 it's already been done (for some connectors, not all)
felixfbecker commentedon Dec 7, 2016
No, if there is a bug in the type parser, it should be fixed in
data-types.js, not by calling$refreshTypeParsers()later on from user land code.janmeier commentedon Dec 13, 2016
PR welcome, the relevant code is here (needs to add a parse function) https://github.com/sequelize/sequelize/blob/v3.27.0/lib/dialects/mssql/data-types.js#L102
pibone commentedon Jan 24, 2017
For the sake of fixing this malefic bug, some concerns I have:
zhupengfeivip commentedon Apr 23, 2018
We use mariadb10.0.3, without datatime2 and datetimeoffset data type, how to solve it?Is this not a fix for sequelize?I see this property, does it work?
DialectOptions: {
UseUTC: true, // ->Add this line. For reading from database.
},