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

Commit f27f9a27 by Jan Aagaard Meier

Support for composite primary keys in upsert. Closes #3065

1 parent cc732433
# Next # Next
- [BUG] Fixed `field` support for `increment` and `decrement`. - [BUG] Fixed `field` support for `increment` and `decrement`.
- [FEATURE/BUG] Raw queries always return all results (including affected rows etc). This means you should change all promise listeners on `sequelize.query` to use `.spread` instead of `.then`, unless you are passing a query type. - [FEATURE/BUG] Raw queries always return all results (including affected rows etc). This means you should change all promise listeners on `sequelize.query` to use `.spread` instead of `.then`, unless you are passing a query type.
- [BUG] Support for composite primary keys in upsert [#3065](https://github.com/sequelize/sequelize/pull/3065)
#### Backwards compatibility changes #### Backwards compatibility changes
- The default query type for `sequelize.query` is now `RAW` - this means that two arguments (results and metadata) will be returned by default and you should use `.spread` - The default query type for `sequelize.query` is now `RAW` - this means that two arguments (results and metadata) will be returned by default and you should use `.spread`
......
...@@ -485,9 +485,15 @@ module.exports = (function() { ...@@ -485,9 +485,15 @@ module.exports = (function() {
, updateValues , updateValues
, attributes = Object.keys(values); , attributes = Object.keys(values);
if (values[model.primaryKeyField]) { where = {};
where = {}; for (var i = 0; i < model.primaryKeyAttributes.length; i++) {
where[model.primaryKeyField] = values[model.primaryKeyField]; var key = model.primaryKeyAttributes[i];
if(key in values){
where[key] = values[key];
}
}
if (!Utils._.isEmpty(where)) {
wheres.push(where); wheres.push(where);
} }
......
...@@ -87,6 +87,60 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -87,6 +87,60 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
it('works with upsert on a composite primary key', function() {
var User = this.sequelize.define('user', {
a: {
type: Sequelize.STRING,
primaryKey: true,
},
b: {
type: Sequelize.STRING,
primaryKey: true,
},
username: DataTypes.STRING,
});
return User.sync({ force: true }).bind(this).then(function () {
return Promise.all([
// Create two users
User.upsert({ a: 'a', b: 'b', username: 'john' }),
User.upsert({ a: 'a', b: 'a', username: 'curt' }),
]);
}).spread(function(created1, created2) {
if (dialect === 'sqlite') {
expect(created1).not.to.be.defined;
expect(created2).not.to.be.defined;
} else {
expect(created1).to.be.ok;
expect(created2).to.be.ok;
}
return Promise.delay(1000).bind(this).then(function() {
// Update the first one
return User.upsert({ a: 'a', b: 'b', username: 'doe' });
});
}).then(function(created) {
if (dialect === 'sqlite') {
expect(created).not.to.be.defined;
} else {
expect(created).not.to.be.ok;
}
return User.find({ where: { a: 'a', b: 'b' }});
}).then(function (user1) {
expect(user1.createdAt).to.be.defined;
expect(user1.username).to.equal('doe');
expect(user1.updatedAt).to.be.afterTime(user1.createdAt);
return User.find({ where: { a: 'a', b: 'a' }});
}).then(function (user2) {
// The second one should not be updated
expect(user2.createdAt).to.be.defined;
expect(user2.username).to.equal('curt');
expect(user2.updatedAt).to.equalTime(user2.createdAt);
});
});
it('supports validations', function () { it('supports validations', function () {
var User = this.sequelize.define('user', { var User = this.sequelize.define('user', {
email: { email: {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!