1+ -- Now what we need is a function that gets us from
2+ -- relationship -> view_key_dependency -> relationship
3+
4+ -- Adds M2O and O2O relationships for views to tables, tables to views, and views to views. The example below is taken from the test fixtures, but the views names/colnames were modified.
5+ -- allM2OandO2ORels sample query result--
6+ -- private | personnages | private | actors | personnages_role_id_fkey | {"(role_id,id)"}
7+
8+ -- allViewsKeyDependencies sample query result--
9+ -- private | personnages | test | personnages_view | personnages_role_id_fkey | f | {"(role_id,roleId)"}
10+ -- private | actors | test | actors_view | personnages_role_id_fkey | f_ref | {"(id,actorId)"}
11+
12+ -- --this function result--
13+ -- test | personnages_view | private | actors | personnages_role_id_fkey | f | {"(roleId,id)"} | viewTableM2O
14+ -- private | personnages | test | actors_view | personnages_role_id_fkey | f_ref | {"(role_id,actorId)"} | tableViewM2O
15+ -- test | personnages_view | test | actors_view | personnages_role_id_fkey | f,r_ref | {"(roleId,actorId)"} | viewViewM2O
16+
17+
118-- we need this type otherwise the all_m2o_and_o2o_rels fails with
219-- ERROR: column "cols_and_fcols" has pseudo-type record[]
320-- This is due to the array_agg(row(...)) which gives a record[], which is a pseudotype
@@ -17,14 +34,16 @@ CREATE TYPE key_dep_type AS enum(
1734);
1835
1936create type relationship as (
20- table_schema text ,
21- table_name text ,
22- foreign_table_schema text ,
23- foreign_table_name text ,
24- is_self bool ,
25- constraint_name text ,
26- cols_and_fcols ref_pair[],
27- is_one_to_one bool
37+ origin_schema text ,
38+ origin_name text ,
39+ foreign_schema text ,
40+ foreign_name text ,
41+ is_self bool ,
42+ constraint_name text ,
43+ cols_and_fcols ref_pair[],
44+ is_one_to_one bool,
45+ is_origin_view bool,
46+ is_foreign_view bool
2847);
2948
3049-- | A view foreign key or primary key dependency detected on its source table
@@ -72,14 +91,16 @@ pks_uniques_cols as (
7291 group by oid , conrelid
7392)
7493select
75- ns1 .nspname as table_schema ,
76- tab .relname as table_name ,
77- ns2 .nspname as foreign_table_schema ,
78- other .relname as foreign_table_name ,
94+ ns1 .nspname as origin_schema ,
95+ tab .relname as origin_name ,
96+ ns2 .nspname as foreign_schema ,
97+ other .relname as foreign_name ,
7998 traint .conrelid = traint .confrelid as is_self,
8099 traint .conname as constraint_name,
81100 column_info .cols_and_fcols ,
82- (column_info .cols in (select cols from pks_uniques_cols where conrelid = traint .conrelid )) as one_to_one
101+ (column_info .cols in (select cols from pks_uniques_cols where conrelid = traint .conrelid )) as one_to_one,
102+ false as is_origin_view,
103+ false as is_foreign_view
83104from pg_constraint traint
84105join lateral (
85106 select
0 commit comments