diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cbabb7d4..fe5b3c561 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - #2348, Add `db-pool-acquisition-timeout` configuration option, time in seconds to wait to acquire a connection. - @robx - #2428, Fix opening an empty transaction on failed resource embedding - @steve-chavez - #2455, Fix embedding the same table multiple times - @steve-chavez + - #2518, Fix a regression when embedding views where base tables have a different column order for FK columns - @wolfgangwalther ### Deprecated diff --git a/src/PostgREST/SchemaCache.hs b/src/PostgREST/SchemaCache.hs index aea2ebe5b..16341facf 100644 --- a/src/PostgREST/SchemaCache.hs +++ b/src/PostgREST/SchemaCache.hs @@ -679,9 +679,9 @@ allM2OandO2ORels pgVer = FROM pg_constraint traint JOIN LATERAL ( SELECT - array_agg(row(cols.attname, refs.attname) order by cols.attnum) AS cols_and_fcols, - jsonb_agg(cols.attname order by cols.attnum) AS cols - FROM ( SELECT unnest(traint.conkey) AS col, unnest(traint.confkey) AS ref) _ + array_agg(row(cols.attname, refs.attname) order by ord) AS cols_and_fcols, + jsonb_agg(cols.attname order by ord) AS cols + FROM unnest(traint.conkey, traint.confkey) WITH ORDINALITY AS _(col, ref, ord) JOIN pg_attribute cols ON cols.attrelid = traint.conrelid AND cols.attnum = col JOIN pg_attribute refs ON refs.attrelid = traint.confrelid AND refs.attnum = ref ) AS column_info ON TRUE @@ -757,8 +757,10 @@ allViewsKeyDependencies = contype::text as contype, conname, conrelid as resorigtbl, - unnest(conkey) as resorigcol + col as resorigcol, + ord from pg_constraint + left join lateral unnest(conkey) with ordinality as _(col, ord) on true where contype IN ('p', 'f') union -- fk referenced col @@ -766,8 +768,10 @@ allViewsKeyDependencies = concat(contype, '_ref') as contype, conname, confrelid, - unnest(confkey) + col, + ord from pg_constraint + left join lateral unnest(confkey) with ordinality as _(col, ord) on true where contype='f' ), views as ( @@ -897,7 +901,7 @@ allViewsKeyDependencies = rec.view_name, pks_fks.conname as constraint_name, pks_fks.contype as constraint_type, - array_agg(row(col.attname, vcol.attname) order by col.attnum) as column_dependencies + array_agg(row(col.attname, vcol.attname) order by pks_fks.ord) as column_dependencies from recursion rec join pg_class tbl on tbl.oid = rec.resorigtbl join pg_attribute col on col.attrelid = tbl.oid and col.attnum = rec.resorigcol diff --git a/test/spec/Feature/Query/QuerySpec.hs b/test/spec/Feature/Query/QuerySpec.hs index c63d8c028..8cc2b23dc 100644 --- a/test/spec/Feature/Query/QuerySpec.hs +++ b/test/spec/Feature/Query/QuerySpec.hs @@ -739,6 +739,9 @@ spec actualPgVersion = do {"tournament":"tournament_3","player_view":{"first_name":"first_name_3"}}] |] { matchHeaders = [matchContentTypeJson] } + it "works when embedding two views that refer to tables with different column ordering" $ + get "/v1?select=v2(*)" `shouldRespondWith` 200 + it "can embed a view that has group by" $ get "/projects_count_grouped_by?select=number_of_projects,client:clients(name)&order=number_of_projects" `shouldRespondWith` [json| diff --git a/test/spec/fixtures/schema.sql b/test/spec/fixtures/schema.sql index 75a534ce0..e24445481 100644 --- a/test/spec/fixtures/schema.sql +++ b/test/spec/fixtures/schema.sql @@ -2859,3 +2859,21 @@ create or replace function jsbaz(fee) returns setof baz as $$ join johnsmith js on js.baz_id = b.baz_id where js.fee_id = $1.fee_id $$ stable language sql; + + +-- issue https://github.com/PostgREST/postgrest/issues/2518 +create table a ( + primary key (c1, c2), + c1 int, + c2 bool +); + +create table b ( + c2 bool, + c1 int, + foreign key (c1, c2) references a +); + +create view test.v1 as table test.a; + +create view test.v2 as table test.b;