@@ -314,8 +314,7 @@ describe('/tables', async () => {
314
314
is_nullable : false ,
315
315
comment : 'foo' ,
316
316
// Currently no way to test these:
317
- // isPrimaryKey: true,
318
- // isUnique: true,
317
+ // is_unique: true
319
318
} )
320
319
321
320
const { data : columns } = await axios . get ( `${ URL } /columns` )
@@ -330,6 +329,33 @@ describe('/tables', async () => {
330
329
await axios . delete ( `${ URL } /columns/${ newTable . id } .1` )
331
330
await axios . delete ( `${ URL } /tables/${ newTable . id } ` )
332
331
} )
332
+ it ( 'POST /columns for primary key' , async ( ) => {
333
+ const { data : newTable } = await axios . post ( `${ URL } /tables` , { name : 'foo' } )
334
+ await axios . post ( `${ URL } /columns` , {
335
+ table_id : newTable . id ,
336
+ name : 'bar' ,
337
+ type : 'int2' ,
338
+ is_primary_key : true ,
339
+ } )
340
+
341
+ // https://wiki.postgresql.org/wiki/Retrieve_primary_key_columns
342
+ const { data : primaryKeys } = await axios . post (
343
+ `${ URL } /query` ,
344
+ { query : `
345
+ SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type
346
+ FROM pg_index i
347
+ JOIN pg_attribute a ON a.attrelid = i.indrelid
348
+ AND a.attnum = ANY(i.indkey)
349
+ WHERE i.indrelid = '${ newTable . name } '::regclass
350
+ AND i.indisprimary;
351
+ ` }
352
+ )
353
+ assert . equal ( primaryKeys . length , 1 )
354
+ assert . equal ( primaryKeys [ 0 ] . attname , 'bar' )
355
+
356
+ await axios . delete ( `${ URL } /columns/${ newTable . id } .1` )
357
+ await axios . delete ( `${ URL } /tables/${ newTable . id } ` )
358
+ } )
333
359
it ( 'POST /columns array type' , async ( ) => {
334
360
const { data : newTable } = await axios . post ( `${ URL } /tables` , { name : 'a' } )
335
361
await axios . post ( `${ URL } /columns` , {
0 commit comments