From 44dd73adccb4517c1a341a6a16f7bd71d8cf05b5 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 29 Oct 2022 11:28:04 +0200 Subject: [PATCH] fix: Embedding views with partial FK references broken This is a regression introduced in d2719420f46a537a75d76a4666f8bffec582d4f8. Fixes #2548 Signed-off-by: Wolfgang Walther --- CHANGELOG.md | 4 ++++ src/PostgREST/SchemaCache.hs | 6 +++++- .../Feature/Query/EmbedDisambiguationSpec.hs | 3 +++ test/spec/fixtures/schema.sql | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 736846a1e..3e701c70f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased +### Fixed + + - #2548, Fix regression when embedding views with partial references to multi column FKs - @wolfgangwalther + ## [10.1.0] - 2022-10-28 ### Added diff --git a/src/PostgREST/SchemaCache.hs b/src/PostgREST/SchemaCache.hs index 4c6fbc478..e6da29c66 100644 --- a/src/PostgREST/SchemaCache.hs +++ b/src/PostgREST/SchemaCache.hs @@ -804,6 +804,7 @@ allViewsKeyDependencies = select contype::text as contype, conname, + array_length(conkey, 1) as ncol, conrelid as resorigtbl, col as resorigcol, ord @@ -815,6 +816,7 @@ allViewsKeyDependencies = select concat(contype, '_ref') as contype, conname, + array_length(confkey, 1) as ncol, confrelid, col, ord @@ -980,7 +982,9 @@ allViewsKeyDependencies = join pg_class tbl on tbl.oid = rep.resorigtbl join pg_attribute col on col.attrelid = tbl.oid and col.attnum = rep.resorigcol join pg_namespace sch on sch.oid = tbl.relnamespace - group by sch.nspname, tbl.relname, rep.view_schema, rep.view_name, pks_fks.conname, pks_fks.contype + group by sch.nspname, tbl.relname, rep.view_schema, rep.view_name, pks_fks.conname, pks_fks.contype, pks_fks.ncol + -- make sure we only return key for which all columns are referenced in the view - no partial PKs or FKs + having ncol = array_length(array_agg(row(col.attname, view_columns) order by pks_fks.ord), 1) |] param :: HE.Value a -> HE.Params a diff --git a/test/spec/Feature/Query/EmbedDisambiguationSpec.hs b/test/spec/Feature/Query/EmbedDisambiguationSpec.hs index 1535ed4e0..50a56c283 100644 --- a/test/spec/Feature/Query/EmbedDisambiguationSpec.hs +++ b/test/spec/Feature/Query/EmbedDisambiguationSpec.hs @@ -563,3 +563,6 @@ spec = } ]|] { matchHeaders = [matchContentTypeJson] } + + it "should not expose hidden FKs" $ + get "/va?select=vb(*)" `shouldRespondWith` 200 diff --git a/test/spec/fixtures/schema.sql b/test/spec/fixtures/schema.sql index e94621ba7..a4593b951 100644 --- a/test/spec/fixtures/schema.sql +++ b/test/spec/fixtures/schema.sql @@ -2980,3 +2980,19 @@ select parent.parent as grandparent, join public.i2459_self_t as child on child.parent = parent.id where child.type = 'B'; + +-- issue https://github.com/PostgREST/postgrest/issues/2548 +CREATE TABLE public.ta ( + a1 INT PRIMARY KEY, + a2 INT, + UNIQUE (a1, a2) +); + +CREATE TABLE public.tb ( + b1 INT REFERENCES public.ta (a1), + b2 INT, + FOREIGN KEY (b1, b2) REFERENCES public.ta (a1, a2) +); + +CREATE VIEW test.va AS SELECT a1 FROM public.ta; +CREATE VIEW test.vb AS SELECT b1 FROM public.tb;