Skip to content

Commit a5becce

Browse files
author
Lyman Gillispie
committed
Added support for REFERENCES in CREATE TABLE and ALTER TABLE operations.
1 parent 45d58a2 commit a5becce

4 files changed

Lines changed: 154 additions & 0 deletions

File tree

lib/dialect/postgres.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,27 @@ Postgres.prototype.visitColumn = function(columnNode) {
664664
// creating a column as a primary key
665665
txt.push(' PRIMARY KEY');
666666
}
667+
668+
if (!!columnNode.references) {
669+
assert.equal(typeof (columnNode.references), 'object',
670+
'references is not a object for column ' + columnNode.name +
671+
' (REFERENCES statements within CREATE TABLE and ADD COLUMN statements' +
672+
' require refrences to be expressed as an object)');
673+
674+
//Empty refrence objects are ok
675+
if (Object.keys(columnNode.references).length > 0){
676+
assert(columnNode.references.table, 'reference.table missing for column ' +
677+
columnNode.name +
678+
' (REFERENCES statements within CREATE TABLE and ADD COLUMN statements' +
679+
' require a table and column)');
680+
assert(columnNode.references.table, 'reference.column missing for column ' +
681+
columnNode.name +
682+
' (REFERENCES statements within CREATE TABLE and ADD COLUMN statements' +
683+
' require a table and column)');
684+
txt.push(' REFERENCES ' + columnNode.references.table + '(' +
685+
columnNode.references.column + ')');
686+
}
687+
}
667688
}
668689
return [txt.join('')];
669690
};

lib/node/column.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = Node.define({
1717
this.dataType = config.dataType;
1818
this.distinct = config.distinct;
1919
this.primaryKey = config.primaryKey;
20+
this.references = config.references;
2021
this.autoGenerated = !!config.autoGenerated;
2122
},
2223
as: function(alias) {

test/dialects/alter-table-tests.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,53 @@ Harness.test({
210210
throws: true
211211
}
212212
});
213+
214+
var post = Table.define({
215+
name: 'post',
216+
columns: [{
217+
name: 'userId',
218+
dataType: 'int',
219+
references: {
220+
table: 'user',
221+
column: 'id'
222+
}
223+
}, {
224+
name: 'picture',
225+
dataType: 'varchar(100)',
226+
references: {}
227+
}]
228+
});
229+
230+
Harness.test({
231+
query: post.alter().addColumn(post.userId),
232+
pg: {
233+
text : 'ALTER TABLE "post" ADD COLUMN "userId" int REFERENCES user(id)',
234+
string: 'ALTER TABLE "post" ADD COLUMN "userId" int REFERENCES user(id)'
235+
},
236+
sqlite: {
237+
text : 'ALTER TABLE "post" ADD COLUMN "userId" int REFERENCES user(id)',
238+
string: 'ALTER TABLE "post" ADD COLUMN "userId" int REFERENCES user(id)'
239+
},
240+
mysql: {
241+
text : 'ALTER TABLE `post` ADD COLUMN `userId` int REFERENCES user(id)',
242+
string: 'ALTER TABLE `post` ADD COLUMN `userId` int REFERENCES user(id)'
243+
},
244+
params: []
245+
});
246+
247+
Harness.test({
248+
query: post.alter().addColumn(post.picture),
249+
pg: {
250+
text : 'ALTER TABLE "post" ADD COLUMN "picture" varchar(100)',
251+
string: 'ALTER TABLE "post" ADD COLUMN "picture" varchar(100)'
252+
},
253+
sqlite: {
254+
text : 'ALTER TABLE "post" ADD COLUMN "picture" varchar(100)',
255+
string: 'ALTER TABLE "post" ADD COLUMN "picture" varchar(100)'
256+
},
257+
mysql: {
258+
text : 'ALTER TABLE `post` ADD COLUMN `picture` varchar(100)',
259+
string: 'ALTER TABLE `post` ADD COLUMN `picture` varchar(100)'
260+
},
261+
params: []
262+
});

test/dialects/create-table-tests.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,85 @@ Harness.test({
144144
string: 'CREATE TABLE `user` (`id` int PRIMARY KEY)'
145145
}
146146
});
147+
148+
Harness.test({
149+
query: Table.define({
150+
name: 'post',
151+
columns: [{
152+
name: 'userId',
153+
dataType: 'int',
154+
references: {
155+
table: 'user',
156+
column: 'id'
157+
}
158+
}]
159+
}).create(),
160+
pg: {
161+
text : 'CREATE TABLE "post" ("userId" int REFERENCES user(id))',
162+
string: 'CREATE TABLE "post" ("userId" int REFERENCES user(id))'
163+
},
164+
sqlite: {
165+
text : 'CREATE TABLE "post" ("userId" int REFERENCES user(id))',
166+
string: 'CREATE TABLE "post" ("userId" int REFERENCES user(id))'
167+
},
168+
mysql: {
169+
text : 'CREATE TABLE `post` (`userId` int REFERENCES user(id))',
170+
string: 'CREATE TABLE `post` (`userId` int REFERENCES user(id))'
171+
},
172+
params: []
173+
});
174+
175+
Harness.test({
176+
query: Table.define({
177+
name: 'picture',
178+
columns: [{
179+
name: 'userId',
180+
dataType: 'int',
181+
references: {
182+
table: 'user',
183+
column: 'id'
184+
}
185+
}, {
186+
name: 'caption',
187+
dataType: 'varchar(100)',
188+
references: {}
189+
}]
190+
}).create(),
191+
pg: {
192+
text : 'CREATE TABLE "picture" ("userId" int REFERENCES user(id), "caption" varchar(100))',
193+
string: 'CREATE TABLE "picture" ("userId" int REFERENCES user(id), "caption" varchar(100))'
194+
},
195+
sqlite: {
196+
text : 'CREATE TABLE "picture" ("userId" int REFERENCES user(id), "caption" varchar(100))',
197+
string: 'CREATE TABLE "picture" ("userId" int REFERENCES user(id), "caption" varchar(100))'
198+
},
199+
mysql: {
200+
text : 'CREATE TABLE `picture` (`userId` int REFERENCES user(id), `caption` varchar(100))',
201+
string: 'CREATE TABLE `picture` (`userId` int REFERENCES user(id), `caption` varchar(100))'
202+
},
203+
params: []
204+
});
205+
206+
Harness.test({
207+
query: Table.define({
208+
name: 'post',
209+
columns: [{
210+
name: 'userId',
211+
dataType: 'int',
212+
references: 'user'
213+
}]
214+
}).create(),
215+
pg: {
216+
text : 'references is not a object for column userId (REFERENCES statements within CREATE TABLE and ADD COLUMN statements require refrences to be expressed as an object)',
217+
throws: true
218+
},
219+
sqlite: {
220+
text : 'references is not a object for column userId (REFERENCES statements within CREATE TABLE and ADD COLUMN statements require refrences to be expressed as an object)',
221+
throws: true
222+
},
223+
mysql: {
224+
text : 'references is not a object for column userId (REFERENCES statements within CREATE TABLE and ADD COLUMN statements require refrences to be expressed as an object)',
225+
throws: true
226+
},
227+
params: []
228+
});

0 commit comments

Comments
 (0)