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

Commit 11a92a2b by Jan Aagaard Meier

Merge pull request #5298 from PhinCo/master

Fix for bug #4755
2 parents 68e61305 0e4e7045
......@@ -495,20 +495,23 @@ QueryInterface.prototype.insert = function(instance, tableName, values, options)
});
};
QueryInterface.prototype.upsert = function(tableName, values, updateValues, model, options) {
QueryInterface.prototype.upsert = function(tableName, valuesByField, updateValues, model, options) {
var wheres = []
, where
, indexFields
, indexes = []
, attributes = Object.keys(values);
, attributes = Object.keys(valuesByField);
where = {};
for (var i = 0; i < model.primaryKeyAttributes.length; i++) {
var key = model.primaryKeyAttributes[i];
if(key in values){
where[key] = values[key];
where = model.primaryKeyAttributes.reduce(function (where, key) {
var attribute = model.rawAttributes[key];
if (attribute.field && attribute.field in valuesByField) {
where[attribute.field] = valuesByField[attribute.field];
} else if (key in valuesByField) {
where[key] = valuesByField[key];
}
}
return where;
}, {} );
if (!Utils._.isEmpty(where)) {
wheres.push(where);
......@@ -536,7 +539,7 @@ QueryInterface.prototype.upsert = function(tableName, values, updateValues, mode
if (Utils._.intersection(attributes, index).length === index.length) {
where = {};
index.forEach(function (field) {
where[field] = values[field];
where[field] = valuesByField[field];
});
wheres.push(where);
}
......@@ -547,7 +550,7 @@ QueryInterface.prototype.upsert = function(tableName, values, updateValues, mode
options.type = QueryTypes.UPSERT;
options.raw = true;
var sql = this.QueryGenerator.upsertQuery(tableName, values, updateValues, where, model.rawAttributes, options);
var sql = this.QueryGenerator.upsertQuery(tableName, valuesByField, updateValues, where, model.rawAttributes, options);
return this.sequelize.query(sql, options).then(function (rowCount) {
if (rowCount === undefined) {
return rowCount;
......
......@@ -31,6 +31,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
blob: DataTypes.BLOB
});
this.ModelWithFieldPK = this.sequelize.define('ModelWithFieldPK', {
userId: {
field: 'user_id',
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
foo:{
type: DataTypes.STRING,
unique: true
}
});
return this.sequelize.sync({ force: true });
});
......@@ -226,6 +239,30 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
it('works with primary key using .field', function () {
return this.ModelWithFieldPK.upsert({ userId: 42, foo: 'first' }).bind(this).then(function(created) {
if (dialect === 'sqlite') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.ok;
}
return this.sequelize.Promise.delay(1000).bind(this).then(function() {
return this.ModelWithFieldPK.upsert({ userId: 42, foo: 'second' });
});
}).then(function(created) {
if (dialect === 'sqlite') {
expect(created).to.be.undefined;
} else {
expect(created).not.to.be.ok;
}
return this.ModelWithFieldPK.findOne({ userId: 42 });
}).then(function(instance) {
expect(instance.foo).to.equal('second');
});
});
it('works with database functions', function() {
return this.User.upsert({ id: 42, username: 'john', foo: this.sequelize.fn('upper', 'mixedCase1')}).bind(this).then(function(created) {
if (dialect === 'sqlite') {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!