不要怂,就是干,撸起袖子干!

Commit 7a73493f by Mick Hansen

fix(model): fixes issue with default values for timestamps, closes #3438

1 parent 050e6c20
...@@ -1776,7 +1776,7 @@ module.exports = (function() { ...@@ -1776,7 +1776,7 @@ module.exports = (function() {
Model.prototype.__getTimestamp = function(attr) { Model.prototype.__getTimestamp = function(attr) {
if (!!this.rawAttributes[attr] && !!this.rawAttributes[attr].defaultValue) { if (!!this.rawAttributes[attr] && !!this.rawAttributes[attr].defaultValue) {
return this.rawAttributes[attr].defaultValue; return Utils.toDefaultValue(this.rawAttributes[attr].defaultValue);
} else { } else {
return Utils.now(this.sequelize.options.dialect); return Utils.now(this.sequelize.options.dialect);
} }
......
...@@ -546,6 +546,40 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -546,6 +546,40 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
it('works with custom timestamps with a default value', function() {
var User = this.sequelize.define('User', {
username: DataTypes.STRING,
date_of_birth: DataTypes.DATE,
email: DataTypes.STRING,
password: DataTypes.STRING,
created_time: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: DataTypes.NOW
},
updated_time: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: DataTypes.NOW
}
}, {
createdAt: 'created_time',
updatedAt: 'updated_time',
tableName: 'users',
underscored: true,
freezeTableName: true,
force: false
});
return this.sequelize.sync({force: true}).then(function() {
return User.create({}, {logging: console.log}).then(function(user) {
expect(user).to.be.ok;
expect(user.created_time).to.be.ok;
expect(user.updated_time).to.be.ok;
});
});
});
if (current.dialect.supports.transactions) { if (current.dialect.supports.transactions) {
it('supports transactions', function() { it('supports transactions', function() {
var self = this; var self = this;
......
'use strict';
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, current = Support.sequelize;
describe(Support.getTestDialectTeaser('Instance'), function() {
describe('build', function () {
it('should popuplate NOW default values', function () {
var Model = current.define('Model', {
created_time: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: DataTypes.NOW
},
updated_time: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: DataTypes.NOW
}
})
, instance;
instance = Model.build({});
expect(instance.get('created_time')).to.be.ok;
expect(instance.get('created_time')).to.be.an.instanceof(Date);
expect(instance.get('updated_time')).to.be.ok;
expect(instance.get('updated_time')).to.be.an.instanceof(Date);
});
});
});
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!