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

Commit dbf28085 by Allan Carroll

Set insertId for non-default auto increment fields

1 parent 46cfe2c4
...@@ -17,6 +17,7 @@ var ModelDefinition = module.exports = function(name, attributes, options) { ...@@ -17,6 +17,7 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
this.addDefaultAttributes() this.addDefaultAttributes()
this.addOptionalClassMethods() this.addOptionalClassMethods()
this.findAutoIncrementField()
} }
Utils.addEventEmitter(ModelDefinition) Utils.addEventEmitter(ModelDefinition)
...@@ -43,6 +44,19 @@ ModelDefinition.prototype.addDefaultAttributes = function() { ...@@ -43,6 +44,19 @@ ModelDefinition.prototype.addDefaultAttributes = function() {
Utils._.map(defaultAttributes, function(value, attr) { self.attributes[attr] = value }) Utils._.map(defaultAttributes, function(value, attr) { self.attributes[attr] = value })
} }
ModelDefinition.prototype.findAutoIncrementField = function() {
var self = this;
this.autoIncrementField = null;
Utils._.map(this.attributes, function(definition, name) {
if (definition && definition.indexOf('auto_increment') >= 0) {
if (self.autoIncrementField) {
throw new Error('Invalid model definition. Only one autoincrement field allowed.')
}
self.autoIncrementField = name;
}
})
}
ModelDefinition.prototype.query = function() { ModelDefinition.prototype.query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg }) var args = Utils._.map(arguments, function(arg, _) { return arg })
, s = this.modelManager.sequelize , s = this.modelManager.sequelize
......
...@@ -34,8 +34,8 @@ Query.prototype.onSuccess = function(query, results, fields) { ...@@ -34,8 +34,8 @@ Query.prototype.onSuccess = function(query, results, fields) {
, self = this , self = this
// add the inserted row id to the instance // add the inserted row id to the instance
if (this.callee && !this.callee.options.hasPrimaryKeys && (query.indexOf('INSERT INTO') == 0) && (results.hasOwnProperty('insertId'))) if (this.callee && (query.indexOf('INSERT INTO') == 0) && (results.hasOwnProperty('insertId')))
this.callee.id = results.insertId this.callee[this.callee.definition.autoIncrementField] = results.insertId
// transform results into real model instances // transform results into real model instances
// return the first real model instance if options.plain is set (e.g. Model.find) // return the first real model instance if options.plain is set (e.g. Model.find)
......
...@@ -37,5 +37,16 @@ module.exports = { ...@@ -37,5 +37,16 @@ module.exports = {
}) })
}) })
}) })
},
'it should set the auto increment field to the insert id': function(exit) {
var User = sequelize.define('User' + config.rand(), {
userid: {type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true, allowNull: false}
})
User.sync({force:true}).on('success', function() {
User.create({}).on('success', function(user) {
assert.eql(user.userid, 1)
exit(function(){})
})
})
} }
} }
\ No newline at end of file
...@@ -75,5 +75,13 @@ module.exports = { ...@@ -75,5 +75,13 @@ module.exports = {
assert.isDefined(User.build().makeItSo) assert.isDefined(User.build().makeItSo)
assert.eql(User.build().makeItSo(), 2) assert.eql(User.build().makeItSo(), 2)
},
'it shouldn\'t allow two auto increment fields': function() {
assert.throws(function () {
var User = sequelize.define('User', {
userid: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
userscore: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
})
})
} }
} }
\ No newline at end of file
module.exports = { module.exports = {
username: 'root' username: 'root'
, password: null , password: 'password'
, database: 'sequelize_test' , database: 'sequelize_test'
, host: '127.0.0.1' , host: '127.0.0.1'
, rand: function() { , rand: function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!