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

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
- [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] 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.
......
......@@ -14,7 +14,10 @@ module.exports = (function() {
return false;
}
return match[1].split('_');
return {
indexName: match[1],
fields: match[1].split('_')
};
}
}
};
......
......@@ -71,8 +71,11 @@ module.exports = (function() {
, pkString = primaryKeys.map(function(pk) { return this.quoteIdentifier(pk); }.bind(this)).join(', ');
if (!!options.uniqueKeys) {
Utils._.each(options.uniqueKeys, function(columns) {
values.attributes += ', UNIQUE uniq_' + tableName + '_' + columns.fields.join('_') + ' (' + Utils._.map(columns.fields, self.quoteIdentifier).join(', ') + ')';
Utils._.each(options.uniqueKeys, function(columns, indexName) {
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() {
return false;
}
return match[1].split('_');
return {
indexName: match[1],
fields: match[1].split('_')
};
}
},
......
......@@ -113,7 +113,9 @@ module.exports = (function() {
return false;
}
return match[1].split('_').splice(1);
return {
fields: match[1].split('_').splice(1)
};
}
},
......
......@@ -125,7 +125,9 @@ module.exports = (function() {
return false;
}
return match[1].split(', ');
return {
fields: match[1].split(', ')
};
}
},
......
......@@ -574,13 +574,14 @@ module.exports = (function() {
}).then(function() {
return self.QueryInterface[query].apply(self.QueryInterface, args).catch(function(err) {
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) {
fields = fields.filter(function(f) { return f !== self.Model.tableName; });
Utils._.each(self.__options.uniqueKeys, function(value) {
if (Utils._.isEqual(value.fields, fields) && !!value.msg) {
err = new Error(value.msg);
if (index !== false) {
var fields = index.fields.filter(function(f) { return f !== self.Model.tableName; });
Utils._.each(self.__options.uniqueKeys, function(uniqueKey) {
if (!!uniqueKey.msg && (Utils._.isEqual(uniqueKey.fields, fields)) || uniqueKey.name === index.indexName) {
err = new Error(uniqueKey.msg);
}
});
}
......
......@@ -205,6 +205,7 @@ module.exports = (function() {
self.options.uniqueKeys[idxName] = self.options.uniqueKeys[idxName] || {fields: [], msg: null};
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].name = idxName || false;
}
if (options.primaryKey === true) {
......
......@@ -305,8 +305,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
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*(uniq_UserWithUniqueUsernames_aCol_bCol)?\s*\([`"]?aCol[`"]?, [`"]?bCol[`"]?\)/)
expect(sql).to.match(/UNIQUE\s*(user_and_email)?\s*\([`"]?username[`"]?, [`"]?email[`"]?\)/)
expect(sql).to.match(/UNIQUE\s*(a_and_b)?\s*\([`"]?aCol[`"]?, [`"]?bCol[`"]?\)/)
done()
}))
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!