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

Commit 7764212a by Jan Aagaard Meier

bug(indexes) Set unique: true when type = UNIQUE. Closes #5351

1 parent a49a7c1d
# Future # Future
- [ADDED] `beforeCount` hook [#5209](https://github.com/sequelize/sequelize/pull/5209) - [ADDED] `beforeCount` hook [#5209](https://github.com/sequelize/sequelize/pull/5209)
- [ADDED] `validationFailed` hook [#1626](https://github.com/sequelize/sequelize/issues/1626) - [ADDED] `validationFailed` hook [#1626](https://github.com/sequelize/sequelize/issues/1626)
- [FIXED] Mark index as `unique: true` when `type: 'UNIQUE'`. Fixes [#5351](https://github.com/sequelize/sequelize/issues/5351)
# 3.19.3 # 3.19.3
- [FIXED] `updatedAt` and `createdAt` values are now set before validation [#5367](https://github.com/sequelize/sequelize/pull/5367) - [FIXED] `updatedAt` and `createdAt` values are now set before validation [#5367](https://github.com/sequelize/sequelize/pull/5367)
......
...@@ -662,15 +662,7 @@ var QueryGenerator = { ...@@ -662,15 +662,7 @@ var QueryGenerator = {
options = this.nameIndexes([options], options.prefix)[0]; options = this.nameIndexes([options], options.prefix)[0];
} }
options = Utils._.defaults(options, { options = Model.prototype.$conformIndex(options);
type: '',
parser: null
});
if (options.type.toLowerCase() === 'unique') {
options.unique = true;
delete options.type;
}
if (!this._dialect.supports.index.type) { if (!this._dialect.supports.index.type) {
delete options.type; delete options.type;
...@@ -686,8 +678,6 @@ var QueryGenerator = { ...@@ -686,8 +678,6 @@ var QueryGenerator = {
tableName = this.quoteTable(tableName); tableName = this.quoteTable(tableName);
} }
var concurrently = this._dialect.supports.index.concurrently && options.concurrently ? 'CONCURRENTLY' : undefined var concurrently = this._dialect.supports.index.concurrently && options.concurrently ? 'CONCURRENTLY' : undefined
, ind; , ind;
if (this._dialect.supports.indexViaAlter) { if (this._dialect.supports.indexViaAlter) {
......
...@@ -728,12 +728,27 @@ Model.prototype.init = function(modelManager) { ...@@ -728,12 +728,27 @@ Model.prototype.init = function(modelManager) {
} }
}.bind(this)); }.bind(this));
this.options.indexes = this.options.indexes.map(this.$conformIndex);
this.Instance.prototype.$Model = this.Instance.prototype.$Model =
this.Instance.prototype.Model = this; this.Instance.prototype.Model = this;
return this; return this;
}; };
Model.prototype.$conformIndex = function (index) {
index = _.defaults(index, {
type: '',
parser: null
});
if (index.type && index.type.toLowerCase() === 'unique') {
index.unique = true;
delete index.type;
}
return index;
};
Model.prototype.refreshAttributes = function() { Model.prototype.refreshAttributes = function() {
var self = this var self = this
, attributeManipulation = {}; , attributeManipulation = {};
......
...@@ -23,5 +23,21 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -23,5 +23,21 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(Model.options.indexes[0].fields).to.eql(['data']); expect(Model.options.indexes[0].fields).to.eql(['data']);
expect(Model.options.indexes[0].using).to.equal('gin'); expect(Model.options.indexes[0].using).to.equal('gin');
}); });
it('should set the unique property when type is unique', function () {
var Model = current.define('m', {}, {
indexes: [
{
type: 'unique'
},
{
type: 'UNIQUE'
}
]
});
expect(Model.options.indexes[0].unique).to.eql(true);
expect(Model.options.indexes[1].unique).to.eql(true);
});
}); });
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!