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.

Datetime column in MSSQL migrations

See original GitHub issue

In knex.schema, creating or altering a column using table.dateTime() or table.timestamp() generates a query that uses the datetime type. Microsoft discourages the use of this type for new work and recommends using datetime2 instead, since it’s got considerably better precision, a wider representable range and is ANSI SQL compliant, without using any more storage (both types are 8 bytes wide by default). datetime2 is available since SQL Server 2008, but since I believe other parts of knex generates syntax requiring at least SQL Server 2012, that shouldn’t be a problem. The dominant MSSQL Node driver (Tedious) supports both and returns both as a Javascript Date by default.

Now, this is trivial to work around with table.specificType, but I think knex should default to the recommended best practice. I am willing to create a PR for this, but before I do that I figured I’d check if there are any potential problems I’ve overlooked with such a change. I don’t know what the policy is on backwards compatibility in migrations, either.

In addition to switching to datetime2, MSSQL also has a timezone-aware type, datetimeoffset, which is like datetime2 but including a timezone offset (in Postgres terms it’s a timestamptz). Perhaps a better idea would be to implement the same API as Postgres, that is table.dateTime(columnName, withoutTimezone), but unlike just switching from datetime to datetime2 which is technically incompatible but unlikely to actually break things in practice, changing to timezone-aware by default has a great deal of potential for interesting bugs.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dhensbycommented, Feb 25, 2021

fixed in #2757

0reactions
kblomstercommented, Nov 6, 2017

That sounds reasonable. I’ll take a look at setting up a PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

datetime (Transact-SQL) - SQL Server - Microsoft Learn
Defines a date that is combined with a time of day with fractional seconds that is based on a 24-hour clock. ... Use...
Read more >
Datetime column in MSSQL migrations #2304 - knex ... - GitHub
In knex.schema, creating or altering a column using table.dateTime() or table.timestamp() generates a query that uses the datetime type.
Read more >
How to force migrations to create datetime2 fields instead of ...
Force a datetime2 by adding a precision int to the method creating the datetime column. $table->datetime(4); $table->timestamp(4); ...
Read more >
DATETIME or DATETIME2(3)? - Migration to Microsoft SQL ...
In most cases DATETIME2(3) is more preferred data type for migration from other databases to SQL Server since you can store the exact...
Read more >
SQL Server DateTime data type considerations and limitations
DateTime, Is used to store date and time between 01/01/1753 to 31/12/9999. Minimum unit of time it keeps is milliseconds with an accuracy...
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