Skip to content

Commit 2808409

Browse files
kanthoneybrianc
authored andcommitted
add defaultValue parameter when creating tables (brianc#311)
1 parent f941c74 commit 2808409

4 files changed

Lines changed: 38 additions & 0 deletions

File tree

lib/column.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var Column = function(config) {
2020
direction : new TextNode('DESC')
2121
});
2222
this.dataType = config.dataType;
23+
this.defaultValue = config.defaultValue;
2324
};
2425

2526
// mix in value expression

lib/dialect/postgres.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,9 @@ Postgres.prototype.visitColumn = function(columnNode) {
826826
if (!columnNode.primaryKey && columnNode.unique) {
827827
txt.push(' UNIQUE');
828828
}
829+
if (columnNode.defaultValue !== undefined) {
830+
txt.push(' DEFAULT ' + this._getParameterValue(columnNode.defaultValue));
831+
}
829832
}
830833

831834
if (!!columnNode.references) {

lib/node/column.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = Node.define({
2020
this.distinct = config.distinct;
2121
this.primaryKey = config.primaryKey;
2222
this.notNull = config.notNull;
23+
this.defaultValue = config.defaultValue;
2324
this.references = config.references;
2425
// If subfieldContainer is present, this is a subfield and subfieldContainer
2526
// is the parent Column

test/dialects/create-table-tests.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,39 @@ Harness.test({
248248
}
249249
});
250250

251+
Harness.test({
252+
query: Table.define({
253+
name: 'user',
254+
columns: [{
255+
name: 'id',
256+
dataType: 'int',
257+
primaryKey: true,
258+
notNull: true
259+
}, {
260+
name: 'posts',
261+
dataType: 'int',
262+
notNull: true,
263+
defaultValue: 0
264+
}]
265+
}).create(),
266+
pg: {
267+
text : 'CREATE TABLE "user" ("id" int PRIMARY KEY, "posts" int NOT NULL DEFAULT 0)',
268+
string: 'CREATE TABLE "user" ("id" int PRIMARY KEY, "posts" int NOT NULL DEFAULT 0)'
269+
},
270+
sqlite: {
271+
text : 'CREATE TABLE "user" ("id" int PRIMARY KEY, "posts" int NOT NULL DEFAULT 0)',
272+
string: 'CREATE TABLE "user" ("id" int PRIMARY KEY, "posts" int NOT NULL DEFAULT 0)'
273+
},
274+
mysql: {
275+
text : 'CREATE TABLE `user` (`id` int PRIMARY KEY, `posts` int NOT NULL DEFAULT 0)',
276+
string: 'CREATE TABLE `user` (`id` int PRIMARY KEY, `posts` int NOT NULL DEFAULT 0)'
277+
},
278+
oracle: {
279+
text : 'CREATE TABLE "user" ("id" int PRIMARY KEY, "posts" int NOT NULL DEFAULT 0)',
280+
string: 'CREATE TABLE "user" ("id" int PRIMARY KEY, "posts" int NOT NULL DEFAULT 0)'
281+
}
282+
});
283+
251284
Harness.test({
252285
query: Table.define({
253286
name: 'post',

0 commit comments

Comments
 (0)