Skip to content

Instantly share code, notes, and snippets.

@jgomo3
Created August 9, 2019 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgomo3/bb37c8cb5d6533a1c5ad00aa2ae60186 to your computer and use it in GitHub Desktop.
Save jgomo3/bb37c8cb5d6533a1c5ad00aa2ae60186 to your computer and use it in GitHub Desktop.
Create an edge list of relations being related by foreign key [postgres]
-- 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