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

Commit efa9fe82 by Mick Hansen

fix(model): default values should be used if value is undefined, closes #3288

1 parent b9d42fc9
...@@ -1070,7 +1070,7 @@ module.exports = (function() { ...@@ -1070,7 +1070,7 @@ module.exports = (function() {
if (this.Model._hasDefaultValues) { if (this.Model._hasDefaultValues) {
Utils._.each(this.Model._defaultValues, function(valueFn, key) { Utils._.each(this.Model._defaultValues, function(valueFn, key) {
if (!defaults.hasOwnProperty(key)) { if (defaults[key] === undefined) {
defaults[key] = valueFn(); defaults[key] = valueFn();
} }
}); });
...@@ -1100,7 +1100,7 @@ module.exports = (function() { ...@@ -1100,7 +1100,7 @@ module.exports = (function() {
if (Object.keys(defaults).length) { if (Object.keys(defaults).length) {
for (key in defaults) { for (key in defaults) {
if (!values.hasOwnProperty(key)) { if (values[key] === undefined) {
this.set(key, Utils.toDefaultValue(defaults[key]), defaultsOptions); this.set(key, Utils.toDefaultValue(defaults[key]), defaultsOptions);
} }
} }
......
...@@ -100,6 +100,31 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -100,6 +100,31 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
it('should work with undefined uuid primary key in where', function () {
var User = this.sequelize.define('User', {
id: {
type: DataTypes.UUID,
primaryKey: true,
allowNull: false,
defaultValue: DataTypes.UUIDV4
},
name: {
type: DataTypes.STRING
}
});
return User.sync({force: true}).then(function () {
return User.findOrCreate({
where: {
id: undefined
},
defaults: {
name: Math.random().toString()
}
});
});
});
if (['sqlite', 'mssql'].indexOf(current.dialect.name) === -1) { if (['sqlite', 'mssql'].indexOf(current.dialect.name) === -1) {
it('should not deadlock with no existing entries and no outer transaction', function () { it('should not deadlock with no existing entries and no outer transaction', function () {
var User = this.sequelize.define('User', { var User = this.sequelize.define('User', {
...@@ -457,32 +482,6 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -457,32 +482,6 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
it('works with non-integer primary keys with fields excluding the primary key', function () {
var User = this.sequelize.define('User', {
'id': {
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false
},
'email': {
type: DataTypes.STRING
}
});
return this.sequelize.sync({force: true}).then(function() {
return User.create({
email: Math.random().toString()
}, {
fields: ['email'],
logging: console.log
}).then(function(user) {
expect(user).to.be.ok;
expect(user.get('id')).to.be.ok;
});
});
});
it('should return an error for a unique constraint error', function() { it('should return an error for a unique constraint error', function() {
var User = this.sequelize.define('User', { var User = this.sequelize.define('User', {
'email': { 'email': {
......
...@@ -31,5 +31,24 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -31,5 +31,24 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
expect(instance.get('updated_time')).to.be.ok; expect(instance.get('updated_time')).to.be.ok;
expect(instance.get('updated_time')).to.be.an.instanceof(Date); expect(instance.get('updated_time')).to.be.an.instanceof(Date);
}); });
it('should popuplate explicitely undefined UUID primary keys', function () {
var Model = current.define('Model', {
id: {
type: DataTypes.UUID,
primaryKey: true,
allowNull: false,
defaultValue: DataTypes.UUIDV4
}
})
, instance;
instance = Model.build({
id: undefined
});
expect(instance.get('id')).not.to.be.undefined;
expect(instance.get('id')).to.be.ok;
});
}); });
}); });
\ 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!