Created
August 9, 2019 18:35
-
-
Save jgomo3/bb37c8cb5d6533a1c5ad00aa2ae60186 to your computer and use it in GitHub Desktop.
Create an edge list of relations being related by foreign key [postgres]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Pre: The names of the relations to consider are in a relation named nodes in | |
-- the public schema (it could be parametrized) | |
SELECT | |
tc.table_name watcher | |
, ctu.table_name target | |
FROM | |
information_schema.table_constraints tc | |
INNER JOIN information_schema.constraint_table_usage ctu ON tc.constraint_name = ctu.constraint_name | |
INNER JOIN nodes pt ON tc.table_name = pt."name" | |
WHERE | |
tc.constraint_type = 'FOREIGN KEY' | |
AND tc.table_schema = 'public' | |
AND ctu.table_schema = 'public' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment