44-- and pg doesn't allow it as a view column.
55create type ref_pair as (col text , ref text );
66
7+ -- type for the array of (column_name, view_columns[]), otherwise the creation of the function would fail same as above
8+ CREATE TYPE col_dep AS (
9+ col text ,
10+ view_columns text []
11+ );
12+
13+ CREATE TYPE key_dep_type AS enum(
14+ ' p' , -- pk dependency
15+ ' f_ref' , -- fk dependency
16+ ' f' -- fk reference dependency
17+ );
18+
719create type relationship as (
820 table_schema text ,
921 table_name text ,
@@ -15,6 +27,34 @@ create type relationship as (
1527 is_one_to_one bool
1628);
1729
30+ -- | A view foreign key or primary key dependency detected on its source table
31+ -- Each column of the key could be referenced multiple times in the view, e.g.
32+ --
33+ -- create view projects_view as
34+ -- select
35+ -- id as id_1,
36+ -- id as id_2,
37+ -- id as id_3,
38+ -- name
39+ -- from projects
40+ --
41+ -- In this case, the keyDepCols mapping maps projects.id to all three of the columns:
42+ --
43+ -- [('id', ['id_1', 'id_2', 'id_3'])]
44+ --
45+ -- Depending on key type, we can then choose how to handle this case. Primary keys
46+ -- can arbitrarily choose one of the columns, but for foreign keys we need to create
47+ -- relationships for each possible combinations.
48+ CREATE TYPE view_key_dependency AS (
49+ table_schema text ,
50+ table_name text ,
51+ view_schema text ,
52+ view_name text ,
53+ constraint_name text ,
54+ key_dep key_dep_type,
55+ column_dependencies col_dep[]
56+ );
57+
1858-- same query as PostgREST's allM2OandO2ORels
1959create function all_m2o_and_o2o_rels ()
2060returns setof relationship as $$
@@ -59,47 +99,6 @@ order by traint.conrelid, traint.conname;
5999
60100$$ language sql stable;
61101
62-
63- -- type for the array of (column_name, view_columns[]), otherwise the creation of the function would fail
64- CREATE TYPE col_dep AS (
65- col text ,
66- view_columns text []
67- );
68-
69- CREATE TYPE key_dep_type AS enum(
70- ' p' , -- pk dependency
71- ' f_ref' , -- fk dependency
72- ' f' -- fk reference dependency
73- );
74-
75- -- | A view foreign key or primary key dependency detected on its source table
76- -- Each column of the key could be referenced multiple times in the view, e.g.
77- --
78- -- create view projects_view as
79- -- select
80- -- id as id_1,
81- -- id as id_2,
82- -- id as id_3,
83- -- name
84- -- from projects
85- --
86- -- In this case, the keyDepCols mapping maps projects.id to all three of the columns:
87- --
88- -- [('id', ['id_1', 'id_2', 'id_3'])]
89- --
90- -- Depending on key type, we can then choose how to handle this case. Primary keys
91- -- can arbitrarily choose one of the columns, but for foreign keys we need to create
92- -- relationships for each possible combinations.
93- CREATE TYPE view_key_dependency AS (
94- table_schema text ,
95- table_name text ,
96- view_schema text ,
97- view_name text ,
98- constraint_name text ,
99- key_dep key_dep_type,
100- column_dependencies col_dep[]
101- );
102-
103102-- Same query as PostgREST's allViewsKeyDependencies
104103-- Returns all the views' primary keys and foreign keys dependencies
105104-- query explanation at:
0 commit comments