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

Commit 620d8e77 by Mick Hansen

Merge branch 'pola88-primary_key_on_has_many'

2 parents 2d5cf42c 54655ab2
......@@ -290,7 +290,7 @@ module.exports = (function() {
return Utils._.template(query)(replacements)
},
deleteQuery: function(tableName, where, options, factory) {
deleteQuery: function(tableName, where, options, model) {
options = options || {}
tableName = Utils.removeTicks(this.quoteTable(tableName), '"')
......@@ -305,8 +305,8 @@ module.exports = (function() {
primaryKeys[tableName] = primaryKeys[tableName] || [];
if (!!factory && primaryKeys[tableName].length < 1) {
primaryKeys[tableName] = Object.keys(factory.primaryKeys)
if (!!model && primaryKeys[tableName].length < 1) {
primaryKeys[tableName] = Object.keys(model.primaryKeys)
}
var query = "DELETE FROM <%= table %> WHERE <%= primaryKeys %> IN (SELECT <%= primaryKeysSelection %> FROM <%= table %> WHERE <%= where %><%= limit %>)"
......
......@@ -1278,10 +1278,10 @@ module.exports = (function() {
var attrValueHash = {}
attrValueHash[self._timestampAttributes.deletedAt] = Utils.now()
query = 'bulkUpdate'
args = [self.getTableName(), attrValueHash, where]
args = [self.getTableName(), attrValueHash, where, self]
} else {
query = 'bulkDelete'
args = [self.getTableName(), where, options]
args = [self.getTableName(), where, options, self]
}
var runQuery = function(err, records) {
......
......@@ -565,8 +565,8 @@ module.exports = (function() {
});
}
QueryInterface.prototype.bulkDelete = function(tableName, identifier, options) {
var sql = this.QueryGenerator.deleteQuery(tableName, identifier, Utils._.defaults(options || {}, {limit: null}))
QueryInterface.prototype.bulkDelete = function(tableName, identifier, options, model) {
var sql = this.QueryGenerator.deleteQuery(tableName, identifier, Utils._.defaults(options || {}, {limit: null}), model)
return this.queryAndEmit([sql, null, options], 'bulkDelete', options)
}
......
......@@ -1120,6 +1120,47 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
expect(model.options.uniqueKeys[fk].fields).to.deep.equal([ 'TaskId', 'UserId' ])
})
})
describe('without sync', function() {
beforeEach(function(done) {
var self = this
self.sequelize.queryInterface.createTable('users',{ id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true } , username: DataTypes.STRING, createdAt: DataTypes.DATE, updatedAt: DataTypes.DATE }).success(function() {
self.sequelize.queryInterface.createTable('tasks',{ id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, title: DataTypes.STRING, createdAt: DataTypes.DATE, updatedAt: DataTypes.DATE }).success(function() {
self.sequelize.queryInterface.createTable('users_tasks',{ TaskId: DataTypes.INTEGER, UserId: DataTypes.INTEGER, createdAt: DataTypes.DATE, updatedAt: DataTypes.DATE }).success(function() {
done();
})
})
})
})
it('removes all associations', function(done) {
var self = this;
this.UsersTasks = this.sequelize.define('UsersTasks', {}, { tableName: 'users_tasks' });
self.User.hasMany(self.Task, { through: this.UsersTasks })
self.Task.hasMany(self.User, { through: this.UsersTasks })
expect(Object.keys(self.UsersTasks.primaryKeys)).to.deep.equal(['TaskId', 'UserId'])
self.User.create({username: 'foo'}).success(function(user) {
self.Task.create({title: 'foo'}).success(function(task) {
user.addTask(task).success(function(){
user.setTasks(null).success(function(result) {
expect(result).to.be.ok
done()
}).error(function(error) {
console.log(error);
expect(false).to.be.ok
done()
})
})
})
})
})
})
})
describe('through', function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!