diff --git a/lib/mysql.js b/lib/mysql.js index 3390a56..7e8e680 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -830,6 +830,7 @@ MySQL.prototype.alterTable = function (model, actualFields, actualIndexes, done, if (kind && type) { sql.push('ADD ' + kind + ' INDEX `' + propName + '` (`' + propName + '`) ' + type); } else { + (typeof i === 'object' && i.unique && i.unique === true) && (kind = "UNIQUE"); sql.push('ADD ' + kind + ' INDEX `' + propName + '` ' + type + ' (`' + propName + '`) '); } } @@ -967,6 +968,7 @@ MySQL.prototype.singleIndexSettingsSQL = function (model, prop) { if (kind && type) { return (kind + ' INDEX ' + columnName + ' (' + columnName + ') ' + type); } else { + (typeof i === 'object' && i.unique && i.unique === true) && (kind = "UNIQUE"); return (kind + ' INDEX ' + columnName + ' ' + type + ' (' + columnName + ') '); } }; diff --git a/test/mysql.test.js b/test/mysql.test.js index dfc2198..af1c3dc 100644 --- a/test/mysql.test.js +++ b/test/mysql.test.js @@ -1,6 +1,6 @@ var should = require('./init.js'); -var Post, PostWithStringId, db; +var Post, PostWithStringId, PostWithUniqueTitle, db; describe('mysql', function () { @@ -21,7 +21,12 @@ describe('mysql', function () { 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); done(err); }); @@ -30,7 +35,9 @@ describe('mysql', function () { beforeEach(function (done) { Post.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) { Post.destroyAll(function () { - PostWithStringId.destroyAll(done); + PostWithStringId.destroyAll(function () { + PostWithUniqueTitle.destroyAll(done); + }); }); }); });