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

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() { ...@@ -15,7 +15,8 @@ module.exports = (function() {
freezeTableName: false, freezeTableName: false,
underscored: false, underscored: false,
syncOnAssociation: true, syncOnAssociation: true,
paranoid: false paranoid: false,
whereCollection: null
}, options || {}) }, options || {})
this.name = name this.name = name
...@@ -150,7 +151,10 @@ module.exports = (function() { ...@@ -150,7 +151,10 @@ module.exports = (function() {
if (!options.include[daoName]) { if (!options.include[daoName]) {
options.include[daoName] = this.getAssociationByAlias(daoName).target 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 }) return this.QueryInterface.select(this, this.tableName, options, { type: 'SELECT', hasJoin: hasJoin })
...@@ -161,6 +165,9 @@ module.exports = (function() { ...@@ -161,6 +165,9 @@ module.exports = (function() {
var optcpy = Utils._.clone(options) var optcpy = Utils._.clone(options)
optcpy.attributes = optcpy.attributes || [Utils.addTicks(this.tableName)+".*"] 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' }) return this.QueryInterface.select(this, [this.tableName, joinTableName], optcpy, { type: 'SELECT' })
} }
...@@ -210,7 +217,10 @@ module.exports = (function() { ...@@ -210,7 +217,10 @@ module.exports = (function() {
if (!options.include[daoName]) { if (!options.include[daoName]) {
options.include[daoName] = this.getAssociationByAlias(daoName).target 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 options.limit = 1
......
...@@ -121,8 +121,12 @@ module.exports = (function() { ...@@ -121,8 +121,12 @@ module.exports = (function() {
if(this.isNewRecord) { if(this.isNewRecord) {
return this.QueryInterface.insert(this, this.__factory.tableName, values) return this.QueryInterface.insert(this, this.__factory.tableName, values)
} else { } else {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id;
, tableName = this.__factory.tableName
if (identifier === null && this.__options.whereCollection !== null)
identifier = this.__options.whereCollection;
var tableName = this.__factory.tableName
, query = this.QueryInterface.update(this, tableName, values, identifier) , query = this.QueryInterface.update(this, tableName, values, identifier)
return query return query
......
...@@ -19,9 +19,17 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() { ...@@ -19,9 +19,17 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
touchedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW }, touchedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
aNumber: { type: DataTypes.INTEGER } aNumber: { type: DataTypes.INTEGER }
}) })
self.HistoryLog = sequelize.define('HistoryLog', {
someText: { type: DataTypes.STRING },
aNumber: { type: DataTypes.INTEGER },
aRandomId: { type: DataTypes.INTEGER }
})
}, },
onComplete: function() { 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() { ...@@ -67,6 +75,15 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
done() 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() { 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!