Skip to content

Instantly share code, notes, and snippets.

@code-shoily
Created October 28, 2019 14:30
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 code-shoily/6cb2584b77e05d68e2d383be3c71fa34 to your computer and use it in GitHub Desktop.
Save code-shoily/6cb2584b77e05d68e2d383be3c71fa34 to your computer and use it in GitHub Desktop.
Find all tables that has 'auth_user' as Foreign Key
select
(select
cls.relname
from
pg_class cls
where
cls.oid = cnstr.conrelid) as tbl,
(select
array_agg(attname)
from
pg_attribute
where
attrelid = cnstr.conrelid and ARRAY[attnum] <@ cnstr.conkey) as fk_col,
(select
cls.relname
from
pg_class cls
where
cls.oid = cnstr.confrelid) as target
from pg_constraint cnstr
where
cnstr.confrelid = (select oid from pg_class where relname = 'auth_user');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment