которые по форенкею ссылаются на определенную таблицу?
В консоли ПГ (psql) метакоманда \d <tablename> оно показывает таблицы, на которые ссылается, и которые на неё ссылаются.
можно ли каким-то sql вызвать то же самое?
SELECT * FROM ( SELECT pgc.contype as constraint_type, pgc.conname as constraint_name, ccu.table_schema AS table_schema, kcu.table_name as table_name, CASE WHEN (pgc.contype = 'f') THEN kcu.COLUMN_NAME ELSE ccu.COLUMN_NAME END as column_name, CASE WHEN (pgc.contype = 'f') THEN ccu.TABLE_NAME ELSE (null) END as reference_table, CASE WHEN (pgc.contype = 'f') THEN ccu.COLUMN_NAME ELSE (null) END as reference_col FROM pg_constraint AS pgc JOIN pg_namespace nsp ON nsp.oid = pgc.connamespace JOIN pg_class cls ON pgc.conrelid = cls.oid JOIN information_schema.key_column_usage kcu ON kcu.constraint_name = pgc.conname LEFT JOIN information_schema.constraint_column_usage ccu ON pgc.conname = ccu.CONSTRAINT_NAME AND nsp.nspname = ccu.CONSTRAINT_SCHEMA UNION SELECT null as constraint_type , null as constraint_name , 'public' as "table_schema" , table_name , column_name, null as refrence_table , null as refrence_col FROM information_schema.columns cols Where 1=1 AND table_schema = 'public' and column_name not in( SELECT CASE WHEN (pgc.contype = 'f') THEN kcu.COLUMN_NAME ELSE kcu.COLUMN_NAME END FROM pg_constraint AS pgc JOIN pg_namespace nsp ON nsp.oid = pgc.connamespace JOIN pg_class cls ON pgc.conrelid = cls.oid JOIN information_schema.key_column_usage kcu ON kcu.constraint_name = pgc.conname LEFT JOIN information_schema.constraint_column_usage ccu ON pgc.conname = ccu.CONSTRAINT_NAME AND nsp.nspname = ccu.CONSTRAINT_SCHEMA )) as foo where reference_table = ? and reference_col =? ORDER BY table_name desc;
reference_table - сущность на которую ссылаются reference_col - атрибут на который ссылаются, т.е. 'id' вписать
супер, спасибо огромное))
Обсуждают сегодня