diff --git a/lib/mysql.js b/lib/mysql.js index e9574c3..52292dc 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -408,7 +408,15 @@ MySQL.prototype.fromColumnValue = function(prop, val) { val = String(val); break; case 'Date': - val = new Date(val.toString().replace(/GMT.*$/, 'GMT')); + + // MySQL allows, unless NO_ZERO_DATE is set, dummy date/time entries + // new Date() will return Invalid Date for those, so we need to handle + // those separate. + if (val == '0000-00-00 00:00:00') { + val = null; + } else { + val = new Date(val.toString().replace(/GMT.*$/, 'GMT')); + } break; case 'Boolean': val = Boolean(val); diff --git a/test/migration.test.js b/test/migration.test.js index 9b90cea..43a3b81 100644 --- a/test/migration.test.js +++ b/test/migration.test.js @@ -338,6 +338,23 @@ describe('migrations', function() { }); }); + it('should map zero dateTime into null', function (done) { + + query('INSERT INTO `DateData` ' + + '(`dateTime`, `timestamp`) ' + + 'VALUES("0000-00-00 00:00:00", "0000-00-00 00:00:00") ', + function (err, ret) { + should.not.exists(err); + DateData.findById(ret.insertId, function (err, dateData) { + should(dateData.dateTime) + .be.null(); + should(dateData.timestamp) + .be.null(); + done(); + }); + }); + }); + it('should report errors for automigrate', function() { db.automigrate('XYZ', function(err) { assert(err);