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

Commit 5a1631ee by Sascha Depold

Merge branch 'fracture' of git://github.com/durango/sequelize into durango-fracture

2 parents 322e1bb0 01abe949
......@@ -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
}
......@@ -150,7 +151,10 @@ module.exports = (function() {
if (!options.include[daoName]) {
options.include[daoName] = this.getAssociationByAlias(daoName).target
}
}.bind(this))
}.bind(this));
// whereCollection is used for non-primary key updates
this.options.whereCollection = options.where || null;
}
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 || null;
return this.QueryInterface.select(this, [this.tableName, joinTableName], optcpy, { type: 'SELECT' })
}
......@@ -210,7 +217,10 @@ module.exports = (function() {
if (!options.include[daoName]) {
options.include[daoName] = this.getAssociationByAlias(daoName).target
}
}.bind(this))
}.bind(this));
// whereCollection is used for non-primary key updates
this.options.whereCollection = options.where || null;
}
options.limit = 1
......
......@@ -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
......
......@@ -19,9 +19,17 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
touchedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
aNumber: { type: DataTypes.INTEGER }
})
self.HistoryLog = sequelize.define('HistoryLog', {
someText: { type: DataTypes.STRING },
aNumber: { type: DataTypes.INTEGER },
aRandomId: { type: DataTypes.INTEGER }
})
},
onComplete: function() {
self.User.sync({ force: true }).success(done)
self.User.sync({ force: true }).success(function(){
self.HistoryLog.sync({ force: true }).success(done)
})
}
})
})
......@@ -67,6 +75,15 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
done()
})
})
it('saves a record with no primary key', function(done){
this.HistoryLog.create({ someText: 'Some random text', aNumber: 3, aRandomId: 5 }).success(function(log) {
log.updateAttributes({ aNumber: 5 }).success(function(newLog){
expect(newLog.aNumber).toEqual(5)
done()
})
})
})
})
describe('toJSON', function toJSON() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!