diff --git a/lib/index.js b/lib/index.js index c5f962c..97b04b8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -84,12 +84,12 @@ function findFunctions (pgFunctions, schemaName, tableName) { function findConstraints (pgFkConstraints, schemaName, tableName) { return pgFkConstraints - .filter(fk => fk.source_table === `${schemaName}.${tableName}`) + .filter(fk => (fk.source_schema === schemaName && fk.source_table === tableName)) .reduce((fkConstraints, constraint) => { const fkName = constraint.constraint_name if (!Object.prototype.hasOwnProperty.call(fkConstraints, fkName)) { fkConstraints[fkName] = { - targetTable: constraint.target_table, + targetTable: `${constraint.source_schema}.${constraint.target_table}`, sourceColumns: [constraint.source_column], targetColumns: [constraint.target_column], updateAction: FK_ACTION_CODES[constraint.update_action], diff --git a/lib/queries.js b/lib/queries.js index d89849b..ef846d8 100644 --- a/lib/queries.js +++ b/lib/queries.js @@ -53,7 +53,8 @@ module.exports = function queries () { 'WHERE ns.nspname = ANY($1)', // List of foreign key constraints - 'SELECT constraint_name,split_part(source_table, \'.\', 1) AS source_schema, source_table, source_column, target_table, target_column, update_action, delete_action, match_type FROM ' + + 'SELECT constraint_name,(CASE position(\'.\' in SOURCE_TABLE) WHEN 0 THEN \'public\' ELSE split_part(SOURCE_TABLE, \'.\', 1) END) AS source_schema,' + + 'source_table, source_column, target_table, target_column, update_action, delete_action, match_type FROM ' + '(SELECT constraint_name,source_table::regclass::text AS source_table, source_attr.attname AS source_column, ' + 'target_table::regclass::text, target_attr.attname AS target_column, update_action, delete_action, match_type ' + 'FROM pg_attribute target_attr, pg_attribute source_attr, ' + @@ -67,7 +68,7 @@ module.exports = function queries () { ') query2 ' + 'WHERE target_attr.attnum = target_constraints AND target_attr.attrelid = target_table ' + 'AND source_attr.attnum = source_constraints AND source_attr.attrelid = source_table) AS fk_constraints ' + - 'WHERE split_part(source_table, \'.\', 1) = ANY($1)', + 'WHERE (CASE position(\'.\' in SOURCE_TABLE) WHEN 0 THEN \'public\' ELSE split_part(SOURCE_TABLE, \'.\', 1) END) = ANY($1)', // List of triggers 'SELECT trigger_name, trigger_schema, event_object_schema, event_object_table, event_manipulation, action_condition, action_statement, action_orientation, action_timing ' +