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.

Best way to enable support for old MSSQL

See original GitHub issue

I have been using sequelize with mssql 2012 and things work just fine. However, I need to enable temporary support for using mssql 2005.

I have found 2 issues when using sequelize with mssql 2005:

  1. Datetime datatype toSql returns DATETIME2, which doesn’t seem to be supported in mssql 2005. If changed to DATETIME, it works.
  2. Inserts on tables fail because createdAt and UpdatedAt are inserted with wrong format for mssql 2005 DATETIME.

I have found where to fix the datatype:

/dialects/mssql/data-types.js

DATE.prototype.toSql = function() {
  return 'DATETIME2';
};

Changing DATETIME2 to DATETIME would work for MSSQL 2005. To adjust createdAt and UpdatedAt formats to fit in the DATETIME field, I found some changes on the following file would be required.

/lib/sql-string.js

SqlString.dateToString = function(date, timeZone, dialect) {
  date = moment(date).utcOffset(timeZone);

  if (dialect === 'mysql' || dialect === 'mariadb') {
    return date.format('YYYY-MM-DD HH:mm:ss');
  } else {
    // ZZ here means current timezone, _not_ UTC
    return date.format('YYYY-MM-DD HH:mm:ss.SSS Z');
  }
};

Adding mssql to the first condition fixes the second issue with the dates.

I have been looking on a way to enable this legacy support but not sure how to do it:

  1. Changing the DATE prototype and SqlString.dateToString after referencing Sequelize.
  2. Try to create a custom dialect. I tried to use options.dialectModulePath but doesn’t seem to work for this purpose.

Is there any particular way to extend sequelize to enable this?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
itsravenouscommented, May 6, 2015

I have the same issue and followed (mostly blindly!) the advice above from @janmeier to create a DATE2005 type in a plugin-ish way. It’s working for me so far, although obviously it relies on data-types.js staying in the same place in the Sequelize lib 😃

orm/custom-data-types.js:


var BaseTypes = require('sequelize/lib/data-types'),
    util = require('util');

var DATE2005 = function() {
    if (!(this instanceof DATE2005)) return new DATE2005();
    BaseTypes.DATE.apply(this, arguments);
};
util.inherits(DATE2005, BaseTypes.ABSTRACT);

DATE2005.prototype.toSql = function() {
    return 'DATETIME';
};

module.exports = {
    DATE2005: DATE2005
};

if (!DATE2005.key) DATE2005.key = DATE2005;
if (!DATE2005.extend) {
    DATE2005.extend = function(oldType) {
        return new DATE2005(oldType.options);
    };
}

models/customer.js:

var customDataTypes = require('../orm/data-types');

var Customer = orm.define('tblCustomers', {
    CustomerID: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    Forename: {
        type: Sequelize.STRING
    },
    Surname: {
        type: Sequelize.STRING
    },
    DOB: {
        type: customDataTypes.DATE2005
    }
});

0reactions
mubaidrcommented, Nov 15, 2017

I am having the same issue. Is it possible to enable datetime support for timestamps?

mssql 2012

Read more comments on GitHub >

github_iconTop Results From Across the Web

KB3135244 - TLS 1.2 support for Microsoft SQL Server
This article provides information about the updates that Microsoft is releasing to enable TLS 1.2 support for SQL Server 2017 on Windows, SQL...
Read more >
Little SQL Server Tricks: Support for TLS 1.2 - Improve & Repeat
When we tried our first attempt to disable unsecure protocols, we run into an annoying problem on the servers that run SQL Server....
Read more >
Enabling TLS 1.2 with SQL Server
Click Check Names, select MSSQLSERVER and click OK (If MSSQLSERVER is not found, choose SERVICE instead). ; Click OK on the Select Users...
Read more >
Does Microsoft OLE DB Provider for SQL Server support TLS 1.2
You should be able to install SQL Server Native Client 2012 and use that OLE DB provider with only a connection string change...
Read more >
What SQL Server ODBC driver should be used when TLS ...
If TLS 1.2 is enabled, it is likely due to that Microsoft ODBC Driver 17 for SQL Server is not installed to support...
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