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

You need to sign in or sign up before continuing.
Commit eb8c4569 by Allan Carroll

Fix where clause generation for models with explicit primary keys.

1 parent 46cfe2c4
...@@ -69,7 +69,7 @@ Model.prototype.save = function() { ...@@ -69,7 +69,7 @@ Model.prototype.save = function() {
}) })
return eventEmitter.run() return eventEmitter.run()
} else { } else {
var identifier = this.options.hasPrimaryKeys ? this.definition.primaryKeys : this.id var identifier = this.options.hasPrimaryKeys ? this.primaryKeyValues : this.id
return this.query(QueryGenerator.updateQuery(this.definition.tableName, this.values, identifier)) return this.query(QueryGenerator.updateQuery(this.definition.tableName, this.values, identifier))
} }
} }
......
...@@ -16,6 +16,9 @@ Query.prototype.run = function(query) { ...@@ -16,6 +16,9 @@ Query.prototype.run = function(query) {
database: this.config.database database: this.config.database
}) })
// Save the query text for testing and debugging.
this.sql = query
if(this.options.logging) if(this.options.logging)
console.log('Executing: ' + query) console.log('Executing: ' + query)
......
...@@ -28,7 +28,7 @@ module.exports = { ...@@ -28,7 +28,7 @@ module.exports = {
}) })
}, },
'it should not set primary keys or timestamps': function(exit) { 'it should not set primary keys or timestamps': function(exit) {
User = sequelize.define('User' + config.rand(), { var User = sequelize.define('User' + config.rand(), {
name: Sequelize.STRING, bio: Sequelize.TEXT, identifier: {type: Sequelize.STRING, primaryKey: true} name: Sequelize.STRING, bio: Sequelize.TEXT, identifier: {type: Sequelize.STRING, primaryKey: true}
}) })
...@@ -44,5 +44,18 @@ module.exports = { ...@@ -44,5 +44,18 @@ module.exports = {
}) })
}) })
}) })
},
"it should use the primary keys in the where clause": function(exit) {
var User = sequelize.define('User' + config.rand(), {
name: Sequelize.STRING, bio: Sequelize.TEXT, identifier: {type: Sequelize.STRING, primaryKey: true}
})
User.sync({force:true}).on('success', function() {
User.create({name: 'snafu', identifier: 'identifier'}).on('success', function(user) {
var query = user.updateAttributes({name: 'foobar'})
assert.match(query.sql, /WHERE `identifier`..identifier./)
exit(function(){})
})
})
} }
} }
\ 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!