(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) {
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 + '`) ');
}
}
@ -945,6 +946,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 + ') ');
}
};

View File

@ -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,10 +35,12 @@ describe('mysql', function () {
beforeEach(function (done) {
Post.destroyAll(function () {
PostWithStringId.destroyAll(function () {
PostWithUniqueTitle.destroyAll(function () {
done();
});
});
});
});
it('should allow array or object', function (done) {
Post.create({title: 'a', content: 'AAA', comments: ['1', '2'],
@ -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);
});
});
});
});