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

Commit 4fadfb07 by Jan Aagaard Meier

Use the provided index name if one is given. Closes #1944

1 parent a64fe80d
...@@ -17,6 +17,7 @@ Notice: All 1.7.x changes are present in 2.0.x aswell ...@@ -17,6 +17,7 @@ Notice: All 1.7.x changes are present in 2.0.x aswell
- [BUG] find no longer applies limit: 1 if querying on a primary key, should fix a lot of subquery issues. - [BUG] find no longer applies limit: 1 if querying on a primary key, should fix a lot of subquery issues.
- [BUG] Transactions now use the pool so you will never go over your pool defined connection limit - [BUG] Transactions now use the pool so you will never go over your pool defined connection limit
- [BUG] Fix use of Sequelize.literal in eager loading and when renaming attributes [#1916](https://github.com/sequelize/sequelize/pull/1916) - [BUG] Fix use of Sequelize.literal in eager loading and when renaming attributes [#1916](https://github.com/sequelize/sequelize/pull/1916)
- [BUG] Use the provided name for a unique index if one is given, instead of concating the column names together [#1944](https://github.com/sequelize/sequelize/issues/1944)
- [INTERNALS] `bulkDeleteQuery` was removed from the MySQL / abstract query generator, since it was never used internally. Please use `deleteQuery` instead. - [INTERNALS] `bulkDeleteQuery` was removed from the MySQL / abstract query generator, since it was never used internally. Please use `deleteQuery` instead.
......
...@@ -14,7 +14,10 @@ module.exports = (function() { ...@@ -14,7 +14,10 @@ module.exports = (function() {
return false; return false;
} }
return match[1].split('_'); return {
indexName: match[1],
fields: match[1].split('_')
};
} }
} }
}; };
......
...@@ -71,8 +71,11 @@ module.exports = (function() { ...@@ -71,8 +71,11 @@ module.exports = (function() {
, pkString = primaryKeys.map(function(pk) { return this.quoteIdentifier(pk); }.bind(this)).join(', '); , pkString = primaryKeys.map(function(pk) { return this.quoteIdentifier(pk); }.bind(this)).join(', ');
if (!!options.uniqueKeys) { if (!!options.uniqueKeys) {
Utils._.each(options.uniqueKeys, function(columns) { Utils._.each(options.uniqueKeys, function(columns, indexName) {
values.attributes += ', UNIQUE uniq_' + tableName + '_' + columns.fields.join('_') + ' (' + Utils._.map(columns.fields, self.quoteIdentifier).join(', ') + ')'; if (!Utils._.isString(indexName)) {
indexName = 'uniq_' + tableName + '_' + columns.fields.join('_');
}
values.attributes += ', UNIQUE ' + indexName + ' (' + Utils._.map(columns.fields, self.quoteIdentifier).join(', ') + ')';
}); });
} }
...@@ -102,7 +105,10 @@ module.exports = (function() { ...@@ -102,7 +105,10 @@ module.exports = (function() {
return false; return false;
} }
return match[1].split('_'); return {
indexName: match[1],
fields: match[1].split('_')
};
} }
}, },
......
...@@ -113,7 +113,9 @@ module.exports = (function() { ...@@ -113,7 +113,9 @@ module.exports = (function() {
return false; return false;
} }
return match[1].split('_').splice(1); return {
fields: match[1].split('_').splice(1)
};
} }
}, },
......
...@@ -125,7 +125,9 @@ module.exports = (function() { ...@@ -125,7 +125,9 @@ module.exports = (function() {
return false; return false;
} }
return match[1].split(', '); return {
fields: match[1].split(', ')
};
} }
}, },
......
...@@ -574,13 +574,14 @@ module.exports = (function() { ...@@ -574,13 +574,14 @@ module.exports = (function() {
}).then(function() { }).then(function() {
return self.QueryInterface[query].apply(self.QueryInterface, args).catch(function(err) { return self.QueryInterface[query].apply(self.QueryInterface, args).catch(function(err) {
if (!!self.__options.uniqueKeys && err.code && self.QueryInterface.QueryGenerator.uniqueConstraintMapping.code === err.code) { if (!!self.__options.uniqueKeys && err.code && self.QueryInterface.QueryGenerator.uniqueConstraintMapping.code === err.code) {
var fields = self.QueryInterface.QueryGenerator.uniqueConstraintMapping.map(err.toString()); var index = self.QueryInterface.QueryGenerator.uniqueConstraintMapping.map(err.toString());
if (fields !== false) { if (index !== false) {
fields = fields.filter(function(f) { return f !== self.Model.tableName; }); var fields = index.fields.filter(function(f) { return f !== self.Model.tableName; });
Utils._.each(self.__options.uniqueKeys, function(value) {
if (Utils._.isEqual(value.fields, fields) && !!value.msg) { Utils._.each(self.__options.uniqueKeys, function(uniqueKey) {
err = new Error(value.msg); if (!!uniqueKey.msg && (Utils._.isEqual(uniqueKey.fields, fields)) || uniqueKey.name === index.indexName) {
err = new Error(uniqueKey.msg);
} }
}); });
} }
......
...@@ -205,6 +205,7 @@ module.exports = (function() { ...@@ -205,6 +205,7 @@ module.exports = (function() {
self.options.uniqueKeys[idxName] = self.options.uniqueKeys[idxName] || {fields: [], msg: null}; self.options.uniqueKeys[idxName] = self.options.uniqueKeys[idxName] || {fields: [], msg: null};
self.options.uniqueKeys[idxName].fields.push(attribute); self.options.uniqueKeys[idxName].fields.push(attribute);
self.options.uniqueKeys[idxName].msg = self.options.uniqueKeys[idxName].msg || options.unique.msg || null; self.options.uniqueKeys[idxName].msg = self.options.uniqueKeys[idxName].msg || options.unique.msg || null;
self.options.uniqueKeys[idxName].name = idxName || false;
} }
if (options.primaryKey === true) { if (options.primaryKey === true) {
...@@ -391,7 +392,7 @@ module.exports = (function() { ...@@ -391,7 +392,7 @@ module.exports = (function() {
*/ */
Model.prototype.sync = function(options) { Model.prototype.sync = function(options) {
options = Utils._.extend({}, this.options, options || {}); options = Utils._.extend({}, this.options, options || {});
var self = this var self = this
, attributes = this.tableAttributes; , attributes = this.tableAttributes;
......
...@@ -257,8 +257,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -257,8 +257,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
var titleSetter = sinon.spy() var titleSetter = sinon.spy()
, Task = this.sequelize.define('TaskBuild', { , Task = this.sequelize.define('TaskBuild', {
title: { title: {
type: Sequelize.STRING(50), type: Sequelize.STRING(50),
allowNull: false, allowNull: false,
defaultValue: '' defaultValue: ''
} }
}, { }, {
...@@ -305,8 +305,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -305,8 +305,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
User.sync({ force: true }).on('sql', _.after(2, function(sql) { User.sync({ force: true }).on('sql', _.after(2, function(sql) {
expect(sql).to.match(/UNIQUE\s*(uniq_UserWithUniqueUsernames_username_email)?\s*\([`"]?username[`"]?, [`"]?email[`"]?\)/) expect(sql).to.match(/UNIQUE\s*(user_and_email)?\s*\([`"]?username[`"]?, [`"]?email[`"]?\)/)
expect(sql).to.match(/UNIQUE\s*(uniq_UserWithUniqueUsernames_aCol_bCol)?\s*\([`"]?aCol[`"]?, [`"]?bCol[`"]?\)/) expect(sql).to.match(/UNIQUE\s*(a_and_b)?\s*\([`"]?aCol[`"]?, [`"]?bCol[`"]?\)/)
done() done()
})) }))
}) })
...@@ -793,13 +793,13 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -793,13 +793,13 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
this.User.bulkCreate(data).success(function() { this.User.bulkCreate(data).success(function() {
self.User.update({username: 'Bill'}, {secretValue: '42'}).spread(function(affectedRows) { self.User.update({username: 'Bill'}, {secretValue: '42'}).spread(function(affectedRows) {
expect(affectedRows).to.equal(2) expect(affectedRows).to.equal(2)
done() done()
}) })
self.User.update({username: 'Bill'}, {secretValue: '44'}).spread(function(affectedRows) { self.User.update({username: 'Bill'}, {secretValue: '44'}).spread(function(affectedRows) {
expect(affectedRows).to.equal(0) expect(affectedRows).to.equal(0)
done() done()
}) })
}) })
...@@ -987,14 +987,14 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -987,14 +987,14 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
self.User.destroy({secretValue: '42'}).done(function(err, affectedRows) { self.User.destroy({secretValue: '42'}).done(function(err, affectedRows) {
expect(err).not.to.be.ok expect(err).not.to.be.ok
expect(affectedRows).to.equal(2) expect(affectedRows).to.equal(2)
done() done()
}) })
self.User.destroy({secretValue: '44'}).done(function(err, affectedRows) { self.User.destroy({secretValue: '44'}).done(function(err, affectedRows) {
expect(err).not.to.be.ok expect(err).not.to.be.ok
expect(affectedRows).to.equal(0) expect(affectedRows).to.equal(0)
done() done()
}) })
}) })
...@@ -2024,7 +2024,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -2024,7 +2024,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
if (dialect !== 'sqlite') { if (dialect !== 'sqlite') {
it('supports multiple async transactions', function(done) { it('supports multiple async transactions', function(done) {
this.timeout(25000); this.timeout(25000);
...@@ -2037,7 +2037,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -2037,7 +2037,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}, { }, {
transaction: t transaction: t
}).then(function () { }).then(function () {
return User.findAll({ return User.findAll({
where: { where: {
username: "foo" username: "foo"
} }
...@@ -2045,7 +2045,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -2045,7 +2045,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect(users).to.have.length(0); expect(users).to.have.length(0);
}); });
}).then(function () { }).then(function () {
return User.findAll({ return User.findAll({
where: { where: {
username: "foo" username: "foo"
}, },
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!