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

Commit 2c4c643c by Daniel Durante

Fixes a bug with .destroy() on a DAO record

This only affected Postgres really, but when you did .find(), etc. and then
.destroy() on the actual DAO rather than the DAOFactory, sometimes the
QueryGenerator.deleteQuery() would have trouble finding the primary keys for
that table. This provides additional info to Postgres' deleteQuery() function
just in case.

This commit adds a fourth argument to Postgres' QueryGenerator.deleteQuery() for
declaring a DAOFactory.
1 parent 1c16932d
...@@ -402,7 +402,7 @@ module.exports = (function() { ...@@ -402,7 +402,7 @@ module.exports = (function() {
return Utils._.template(query)(replacements) return Utils._.template(query)(replacements)
}, },
deleteQuery: function(tableName, where, options) { deleteQuery: function(tableName, where, options, factory) {
options = options || {} options = options || {}
if (options.truncate === true) { if (options.truncate === true) {
...@@ -415,6 +415,10 @@ module.exports = (function() { ...@@ -415,6 +415,10 @@ module.exports = (function() {
primaryKeys[tableName] = primaryKeys[tableName] || []; primaryKeys[tableName] = primaryKeys[tableName] || [];
if (!!factory && primaryKeys[tableName].length < 1) {
primaryKeys[tableName] = Object.keys(factory.primaryKeys)
}
var query = "DELETE FROM <%= table %> WHERE <%= primaryKeys %> IN (SELECT <%= primaryKeysSelection %> FROM <%= table %> WHERE <%= where %><%= limit %>)" var query = "DELETE FROM <%= table %> WHERE <%= primaryKeys %> IN (SELECT <%= primaryKeysSelection %> FROM <%= table %> WHERE <%= where %><%= limit %>)"
var pks; var pks;
......
...@@ -481,7 +481,7 @@ module.exports = (function() { ...@@ -481,7 +481,7 @@ module.exports = (function() {
QueryInterface.prototype.delete = function(dao, tableName, identifier) { QueryInterface.prototype.delete = function(dao, tableName, identifier) {
var self = this var self = this
, restrict = false , restrict = false
, sql = self.QueryGenerator.deleteQuery(tableName, identifier) , sql = self.QueryGenerator.deleteQuery(tableName, identifier, null, dao.daoFactory)
// Check for a restrict field // Check for a restrict field
if (!!dao.daoFactory && !!dao.daoFactory.associations) { if (!!dao.daoFactory && !!dao.daoFactory.associations) {
......
...@@ -1000,6 +1000,27 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -1000,6 +1000,27 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
}) })
}) })
it('destroys a record with a primary key of something other than id', function(done) {
var UserDestroy = this.sequelize.define('UserDestroy', {
newId: {
type: DataTypes.STRING,
primaryKey: true
},
email: DataTypes.STRING
})
UserDestroy.sync().success(function() {
UserDestroy.create({newId: '123ABC', email: 'hello'}).success(function() {
UserDestroy.find({where: {email: 'hello'}}).success(function(user) {
user.destroy().on('sql', function(sql) {
expect(sql).to.equal('DELETE FROM "UserDestroys" WHERE "newId" IN (SELECT "newId" FROM "UserDestroys" WHERE "newId"=\'123ABC\' LIMIT 1)')
done()
})
})
})
})
})
it("sets deletedAt property to a specific date when deleting an instance", function(done) { it("sets deletedAt property to a specific date when deleting an instance", function(done) {
var self = this var self = this
this.ParanoidUser.create({ username: 'fnord' }).success(function() { this.ParanoidUser.create({ username: 'fnord' }).success(function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!