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

Commit 642fbbd2 by Mick Hansen

Merge pull request #3709 from XadillaX/master

fix a bug that using `Instance::destroy()` under multiple primary keys
2 parents 4397dbac 8cdf1573
......@@ -836,7 +836,10 @@ module.exports = (function() {
return this.save(_.extend(_.clone(options), {hooks : false}));
} else {
where = {};
where[this.Model.rawAttributes[this.Model.primaryKeyAttribute].field] = this.get(this.Model.primaryKeyAttribute, {raw: true});
var primaryKeys = this.Model.primaryKeyAttributes;
for(var i = 0; i < primaryKeys.length; i++) {
where[this.Model.rawAttributes[primaryKeys[i]].field] = this.get(primaryKeys[i], { raw: true });
}
return this.QueryInterface.delete(this, this.Model.getTableName(options), where, _.defaults(options, { type: QueryTypes.DELETE,limit: null}));
}
}).tap(function() {
......
......@@ -1759,6 +1759,44 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
});
});
});
it('delete a record of multiple primary keys table', function() {
var MultiPrimary = this.sequelize.define('MultiPrimary', {
bilibili: {
type: Support.Sequelize.CHAR(2),
primaryKey: true
},
guruguru: {
type: Support.Sequelize.CHAR(2),
primaryKey: true
}
});
return MultiPrimary.sync({ force: true }).then(function() {
return MultiPrimary.create({ bilibili: 'bl', guruguru: 'gu' }).then(function() {
return MultiPrimary.create({ bilibili: 'bl', guruguru: 'ru' }).then(function(m2) {
return MultiPrimary.findAll().then(function(ms) {
expect(ms.length).to.equal(2);
return m2.destroy({
logging: function(sql) {
expect(sql).to.exist;
expect(sql.toUpperCase().indexOf('DELETE')).to.be.above(-1);
expect(sql.indexOf('ru')).to.be.above(-1);
expect(sql.indexOf('bl')).to.be.above(-1);
}
}).then(function() {
return MultiPrimary.findAll().then(function(ms) {
expect(ms.length).to.equal(1);
expect(ms[0].bilibili).to.equal('bl');
expect(ms[0].guruguru).to.equal('gu');
});
});
});
});
});
});
});
});
describe('restore', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!