Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions lib/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,13 @@ function mixinDiscovery(PostgreSQL) {
// http://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html#getPrimaryKeys(java.lang.String, java.lang.String, java.lang.String)

/*
select tc.table_schema, tc.table_name, kc.column_name
from
information_schema.table_constraints tc
join information_schema.key_column_usage kc
on kc.table_name = tc.table_name and kc.table_schema = tc.table_schema
where
tc.constraint_type = 'PRIMARY KEY'
and kc.position_in_unique_constraint is not null
order by tc.table_schema,
tc.table_name,
kc.position_in_unique_constraint;
SELECT kc.table_schema AS "owner", kc.table_name AS "tableName",
kc.column_name AS "columnName", kc.ordinal_position AS "keySeq",
kc.constraint_name AS "pkName" FROM information_schema.key_column_usage kc
JOIN information_schema.table_constraints tc ON kc.table_name = tc.table_name
AND kc.table_schema = tc.table_schema AND kc.constraint_name = tc.constraint_name
WHERE tc.constraint_type='PRIMARY KEY' AND kc.table_name='inventory'
ORDER BY kc.table_schema, kc.table_name, kc.ordinal_position
*/

/*!
Expand All @@ -210,11 +206,14 @@ function mixinDiscovery(PostgreSQL) {
*/
function queryForPrimaryKeys(owner, table) {
var sql = 'SELECT kc.table_schema AS "owner", '
+ 'kc.table_name AS "tableName", kc.column_name AS "columnName", kc.ordinal_position AS "keySeq", kc.constraint_name AS "pkName" FROM'
+ 'kc.table_name AS "tableName", kc.column_name AS "columnName",'
+ ' kc.ordinal_position AS "keySeq",'
+ ' kc.constraint_name AS "pkName" FROM'
+ ' information_schema.key_column_usage kc'
+ ' JOIN information_schema.table_constraints tc'
+ ' ON kc.table_name = tc.table_name AND kc.table_schema = tc.table_schema'
+ ' WHERE tc.constraint_type=\'PRIMARY KEY\' AND kc.position_in_unique_constraint IS NOT NULL';
+ ' AND kc.constraint_name = tc.constraint_name'
+ ' WHERE tc.constraint_type=\'PRIMARY KEY\'';

if (owner) {
sql += ' AND kc.table_schema=\'' + owner + '\'';
Expand Down
7 changes: 5 additions & 2 deletions test/postgresql.discover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@ describe('Discover model primary keys', function () {
done(err);
} else {
models.forEach(function (m) {
// console.dir(m);
assert(m.tableName === 'product');
assert.deepEqual(m, { owner: 'strongloop',
tableName: 'product',
columnName: 'id',
keySeq: 1,
pkName: 'product_pkey' });
});
done(null, models);
}
Expand Down