Skip to content

Commit d85807f

Browse files
committed
Merge pull request #287 from iamcharliegoddard/Issue-266
Issue-266: on create added UNIQUE column constraint
2 parents 96b0b77 + bc2c205 commit d85807f

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

lib/dialect/postgres.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,9 @@ Postgres.prototype.visitColumn = function(columnNode) {
785785
} else if (columnNode.notNull) {
786786
txt.push(' NOT NULL');
787787
}
788+
if (!columnNode.primaryKey && columnNode.unique) {
789+
txt.push(' UNIQUE')
790+
}
788791
}
789792

790793
if (!!columnNode.references) {

lib/node/column.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = Node.define({
2424
this.subfieldContainer = config.subfieldContainer;
2525
this.subfields = config.subfields;
2626
this.autoGenerated = !!config.autoGenerated;
27+
this.unique = !!config.unique;
2728
},
2829
as: function(alias) {
2930
this.alias = alias;

test/dialects/create-table-tests.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,105 @@ Harness.test({
461461
params: []
462462
});
463463

464+
// UNIQUE COLUMN TESTS
465+
Harness.test({
466+
query: Table.define({
467+
name: 'post',
468+
columns: [{
469+
name: 'id',
470+
dataType: 'int',
471+
//primaryKey: true,
472+
//notNull: true,
473+
unique: true
474+
}]
475+
}).create(),
476+
pg: {
477+
text : 'CREATE TABLE "post" ("id" int UNIQUE)',
478+
string: 'CREATE TABLE "post" ("id" int UNIQUE)'
479+
},
480+
sqlite: {
481+
text : 'CREATE TABLE "post" ("id" int UNIQUE)',
482+
string: 'CREATE TABLE "post" ("id" int UNIQUE)'
483+
},
484+
mysql: {
485+
text : 'CREATE TABLE `post` (`id` int UNIQUE)',
486+
string: 'CREATE TABLE `post` (`id` int UNIQUE)'
487+
},
488+
mssql: {
489+
text : 'CREATE TABLE [post] ([id] int UNIQUE)',
490+
string: 'CREATE TABLE [post] ([id] int UNIQUE)'
491+
},
492+
oracle: {
493+
text : 'CREATE TABLE "post" ("id" int UNIQUE)',
494+
string: 'CREATE TABLE "post" ("id" int UNIQUE)'
495+
},
496+
params: []
497+
});
498+
499+
Harness.test({
500+
query: Table.define({
501+
name: 'post',
502+
columns: [{
503+
name: 'id',
504+
dataType: 'int',
505+
//primaryKey: true,
506+
notNull: true,
507+
unique: true
508+
}]
509+
}).create(),
510+
pg: {
511+
text : 'CREATE TABLE "post" ("id" int NOT NULL UNIQUE)',
512+
string: 'CREATE TABLE "post" ("id" int NOT NULL UNIQUE)'
513+
},
514+
sqlite: {
515+
text : 'CREATE TABLE "post" ("id" int NOT NULL UNIQUE)',
516+
string: 'CREATE TABLE "post" ("id" int NOT NULL UNIQUE)'
517+
},
518+
mysql: {
519+
text : 'CREATE TABLE `post` (`id` int NOT NULL UNIQUE)',
520+
string: 'CREATE TABLE `post` (`id` int NOT NULL UNIQUE)'
521+
},
522+
mssql: {
523+
text : 'CREATE TABLE [post] ([id] int NOT NULL UNIQUE)',
524+
string: 'CREATE TABLE [post] ([id] int NOT NULL UNIQUE)'
525+
},
526+
oracle: {
527+
text : 'CREATE TABLE "post" ("id" int NOT NULL UNIQUE)',
528+
string: 'CREATE TABLE "post" ("id" int NOT NULL UNIQUE)'
529+
},
530+
params: []
531+
});
532+
533+
Harness.test({
534+
query: Table.define({
535+
name: 'post',
536+
columns: [{
537+
name: 'id',
538+
dataType: 'int',
539+
primaryKey: true,
540+
//notNull: true,
541+
unique: true
542+
}]
543+
}).create(),
544+
pg: {
545+
text : 'CREATE TABLE "post" ("id" int PRIMARY KEY)',
546+
string: 'CREATE TABLE "post" ("id" int PRIMARY KEY)'
547+
},
548+
sqlite: {
549+
text : 'CREATE TABLE "post" ("id" int PRIMARY KEY)',
550+
string: 'CREATE TABLE "post" ("id" int PRIMARY KEY)'
551+
},
552+
mysql: {
553+
text : 'CREATE TABLE `post` (`id` int PRIMARY KEY)',
554+
string: 'CREATE TABLE `post` (`id` int PRIMARY KEY)'
555+
},
556+
mssql: {
557+
text : 'CREATE TABLE [post] ([id] int PRIMARY KEY)',
558+
string: 'CREATE TABLE [post] ([id] int PRIMARY KEY)'
559+
},
560+
oracle: {
561+
text : 'CREATE TABLE "post" ("id" int PRIMARY KEY)',
562+
string: 'CREATE TABLE "post" ("id" int PRIMARY KEY)'
563+
},
564+
params: []
565+
});

0 commit comments

Comments
 (0)