(cherry picked from commit a6d31e8)

This commit is contained in:
yogesh 2014-10-16 17:33:12 +05:30
parent 4ca9410fbc
commit 6d5ad5e470
2 changed files with 26 additions and 4 deletions

View File

@ -816,6 +816,7 @@ MySQL.prototype.alterTable = function (model, actualFields, actualIndexes, done,
if (kind && type) { if (kind && type) {
sql.push('ADD ' + kind + ' INDEX `' + propName + '` (`' + propName + '`) ' + type); sql.push('ADD ' + kind + ' INDEX `' + propName + '` (`' + propName + '`) ' + type);
} else { } else {
(typeof i === 'object' && i.unique && i.unique === true) && (kind = "UNIQUE");
sql.push('ADD ' + kind + ' INDEX `' + propName + '` ' + type + ' (`' + propName + '`) '); sql.push('ADD ' + kind + ' INDEX `' + propName + '` ' + type + ' (`' + propName + '`) ');
} }
} }
@ -945,6 +946,7 @@ MySQL.prototype.singleIndexSettingsSQL = function (model, prop) {
if (kind && type) { if (kind && type) {
return (kind + ' INDEX ' + columnName + ' (' + columnName + ') ' + type); return (kind + ' INDEX ' + columnName + ' (' + columnName + ') ' + type);
} else { } else {
(typeof i === 'object' && i.unique && i.unique === true) && (kind = "UNIQUE");
return (kind + ' INDEX ' + columnName + ' ' + type + ' (' + columnName + ') '); return (kind + ' INDEX ' + columnName + ' ' + type + ' (' + columnName + ') ');
} }
}; };

View File

@ -1,6 +1,6 @@
var should = require('./init.js'); var should = require('./init.js');
var Post, PostWithStringId, db; var Post, PostWithStringId, PostWithUniqueTitle, db;
describe('mysql', function () { describe('mysql', function () {
@ -21,7 +21,12 @@ describe('mysql', function () {
content: { type: String } content: { type: String }
}); });
db.automigrate(['PostWithDefaultId', 'PostWithStringId'], function (err) { PostWithUniqueTitle = db.define('PostWithUniqueTitle', {
title: { type: String, length: 255, index: {unique: true} },
content: { type: String }
});
db.automigrate(['PostWithDefaultId', 'PostWithStringId', 'PostWithUniqueTitle'], function (err) {
should.not.exist(err); should.not.exist(err);
done(err); done(err);
}); });
@ -30,7 +35,9 @@ describe('mysql', function () {
beforeEach(function (done) { beforeEach(function (done) {
Post.destroyAll(function () { Post.destroyAll(function () {
PostWithStringId.destroyAll(function () { PostWithStringId.destroyAll(function () {
done(); PostWithUniqueTitle.destroyAll(function () {
done();
});
}); });
}); });
}); });
@ -448,9 +455,22 @@ describe('mysql', function () {
}); });
}); });
it('should not allow duplicate titles', function (done) {
var data = {title: 'a', content: 'AAA'};
PostWithUniqueTitle.create(data, function (err, post) {
should.not.exist(err);
PostWithUniqueTitle.create(data, function (err, post) {
should.exist(err);
done();
});
});
});
after(function (done) { after(function (done) {
Post.destroyAll(function () { Post.destroyAll(function () {
PostWithStringId.destroyAll(done); PostWithStringId.destroyAll(function () {
PostWithUniqueTitle.destroyAll(done);
});
}); });
}); });
}); });