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

Commit 06383526 by Sascha Depold

emit errors if schema constraints invalidate Model#save

1 parent 557a1757
...@@ -101,6 +101,13 @@ ModelDefinition.prototype.build = function(values) { ...@@ -101,6 +101,13 @@ ModelDefinition.prototype.build = function(values) {
, self = this , self = this
instance.definition = this instance.definition = this
Utils._.map(this.attributes, function(definition, name) {
if(typeof instance[name] == 'undefined') {
instance[name] = null
instance.addAttribute(name, null)
}
})
return instance return instance
} }
......
...@@ -20,5 +20,20 @@ module.exports = { ...@@ -20,5 +20,20 @@ module.exports = {
}) })
}) })
}) })
},
'it should raise an error if created object breaks definition constraints': function(exit) {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), {
username: {type: Sequelize.STRING, unique: true},
smth: {type: Sequelize.STRING, allowNull: false}
})
User.sync({force:true}).on('success', function() {
User.create({username: 'foo', smth: null}).on('failure', function(err) {
assert.eql(err.message, "Column 'smth' cannot be null")
User.create({username: 'foo'}).on('failure', function(err) {
assert.eql(err.message, "Column 'smth' cannot be null")
exit()
})
})
})
} }
} }
\ 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!