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

Commit 7287f332 by Daniel Durante

Added the ability to update records without a primary key but used a where optio…

…n when selecting with updateAttribute
1 parent 5fa778ea
Showing with 18 additions and 4 deletions
......@@ -15,7 +15,8 @@ module.exports = (function() {
freezeTableName: false,
underscored: false,
syncOnAssociation: true,
paranoid: false
paranoid: false,
whereCollection: null
}, options || {})
this.name = name
......@@ -54,7 +55,7 @@ module.exports = (function() {
this.primaryKeys = {};
Utils._.each(this.attributes, function(dataTypeString, attributeName) {
// If you don't specify a valid data type lets help you debug it
if( dataTypeString === undefined ) throw new Error("Unrecognized data type for field " + attributeName );
if( dataTypeString === undefined ) throw new Error("Unrecognized data type for field " + attributeName );
if((attributeName != 'id') && (dataTypeString.indexOf('PRIMARY KEY') !== -1)) {
self.primaryKeys[attributeName] = dataTypeString
}
......@@ -153,6 +154,9 @@ module.exports = (function() {
}.bind(this))
}
// whereCollection is used for non-primary key updates
this.options.whereCollection = optcpy.where || undefined;
return this.QueryInterface.select(this, this.tableName, options, { type: 'SELECT', hasJoin: hasJoin })
}
......@@ -161,6 +165,9 @@ module.exports = (function() {
var optcpy = Utils._.clone(options)
optcpy.attributes = optcpy.attributes || [Utils.addTicks(this.tableName)+".*"]
// whereCollection is used for non-primary key updates
this.options.whereCollection = optcpy.where || undefined;
return this.QueryInterface.select(this, [this.tableName, joinTableName], optcpy, { type: 'SELECT' })
}
......@@ -215,6 +222,9 @@ module.exports = (function() {
options.limit = 1
// whereCollection is used for non-primary key updates
this.options.whereCollection = optcpy.where || undefined;
return this.QueryInterface.select(this, this.tableName, options, { plain: true, type: 'SELECT', hasJoin: hasJoin })
}
......
......@@ -121,8 +121,12 @@ module.exports = (function() {
if(this.isNewRecord) {
return this.QueryInterface.insert(this, this.__factory.tableName, values)
} else {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
, tableName = this.__factory.tableName
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id;
if (identifier === null && this.__options.whereCollection !== null)
identifier = this.__options.whereCollection;
var tableName = this.__factory.tableName
, query = this.QueryInterface.update(this, tableName, values, identifier)
return query
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!