{"id":338,"date":"2016-06-14T15:38:32","date_gmt":"2016-06-14T15:38:32","guid":{"rendered":"https:\/\/www.tech.shinynewthings.com\/?p=338"},"modified":"2016-06-14T15:38:32","modified_gmt":"2016-06-14T15:38:32","slug":"use-postgres-to-check-for-missing-foreign-keys-psql","status":"publish","type":"post","link":"https:\/\/www.tech.shinynewthings.com\/?p=338","title":{"rendered":"Use Postgres to check for missing foreign keys &#8211; psql"},"content":{"rendered":"<p><code>select Distinct column_name, t1.tablename FROM information_schema.columns, (select * from pg_tables where schemaname='public' AND tablename = 'products') t1 WHERE table_name = 'projects' and column_name like '%_id' and column_name not in (<\/p>\n<p>(SELECT<br \/>\n  (SELECT a.attname FROM pg_attribute a WHERE a.attrelid = m.oid AND a.attnum = o.conkey[1] AND a.attisdropped = false) AS source_column<br \/>\n FROM<br \/>\n  pg_constraint o LEFT JOIN pg_class c ON c.oid = o.conrelid<br \/>\n  LEFT JOIN pg_class f ON f.oid = o.confrelid LEFT JOIN pg_class m ON m.oid = o.conrelid<br \/>\nWHERE<br \/>\n  o.contype = 'f' AND o.conrelid IN (SELECT oid FROM pg_class c WHERE c.relkind = 'r') AND   m.relname = 'products'))<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>select Distinct column_name, t1.tablename FROM information_schema.columns, (select * from pg_tables where schemaname=&#8217;public&#8217; AND tablename = &#8216;products&#8217;) t1 WHERE table_name = &#8216;projects&#8217; and column_name like &#8216;%_id&#8217; and column_name not in ( (SELECT (SELECT a.attname FROM pg_attribute a WHERE a.attrelid = m.oid AND a.attnum = o.conkey[1] AND a.attisdropped = false) AS source_column FROM pg_constraint o LEFT JOIN [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-338","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=338"}],"version-history":[{"count":1,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/338\/revisions"}],"predecessor-version":[{"id":339,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/338\/revisions\/339"}],"wp:attachment":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}