Skip to content

Commit

Permalink
fix: Expose PKs from all tables in a view
Browse files Browse the repository at this point in the history
Fixes a regression in d271942.

Resolves PostgREST#2458

Signed-off-by: Wolfgang Walther <[email protected]>
  • Loading branch information
wolfgangwalther committed Oct 24, 2022
1 parent 2158f3d commit 6fe1617
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- #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
- #2458, Fix a regression with the location header when inserting into views with PKs from multiple tables - @wolfgangwalther

### Deprecated

Expand Down
4 changes: 2 additions & 2 deletions src/PostgREST/SchemaCache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ addViewPrimaryKeys tabs keyDeps =
else tbl) <$> tabs
where
findViewPKCols sch vw =
maybe [] (\(ViewKeyDependency _ _ _ _ pkCols) -> snd <$> pkCols) $
find (\(ViewKeyDependency _ viewQi _ dep _) -> dep == PKDep && viewQi == QualifiedIdentifier sch vw) keyDeps
concatMap (\(ViewKeyDependency _ _ _ _ pkCols) -> snd <$> pkCols) $
filter (\(ViewKeyDependency _ viewQi _ dep _) -> dep == PKDep && viewQi == QualifiedIdentifier sch vw) keyDeps

allTables :: PgVersion -> Bool -> SQL.Statement [Schema] TablesMap
allTables pgVer =
Expand Down
17 changes: 14 additions & 3 deletions test/spec/Feature/Query/InsertSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ spec actualPgVersion = do
}

describe "Inserting into VIEWs" $ do
context "requesting no representation" $
context "requesting no representation" $ do
it "succeeds with 201" $
post "/compound_pk_view"
[json|{"k1":1,"k2":"test","extra":2}|]
Expand All @@ -602,6 +602,17 @@ spec actualPgVersion = do
, matchHeaderAbsent hLocation ]
}

it "returns a location header with pks from both tables" $
request methodPost "/with_multiple_pks" [("Prefer", "return=headers-only")]
[json|{"pk1":1,"pk2":2}|]
`shouldRespondWith`
""
{ matchStatus = 201
, matchHeaders = [ matchHeaderAbsent hContentType
, "Location" <:> "/with_multiple_pks?pk1=eq.1&pk2=eq.2"
, "Content-Range" <:> "*/*" ]
}

context "requesting header only representation" $ do
it "returns a location header with a composite PK col" $
request methodPost "/compound_pk_view" [("Prefer", "return=headers-only")]
Expand All @@ -614,13 +625,13 @@ spec actualPgVersion = do
, "Content-Range" <:> "*/*" ]
}

it "returns location header with a single PK col" $
it "should not throw and return location header when a PK is null" $
request methodPost "/test_null_pk_competitors_sponsors" [("Prefer", "return=headers-only")]
[json|{"id":1}|]
`shouldRespondWith`
""
{ matchStatus = 201
, matchHeaders = [ matchHeaderAbsent hContentType
, "Location" <:> "/test_null_pk_competitors_sponsors?id=eq.1"
, "Location" <:> "/test_null_pk_competitors_sponsors?id=eq.1&sponsor_id=is.null"
, "Content-Range" <:> "*/*" ]
}
19 changes: 19 additions & 0 deletions test/spec/fixtures/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2877,3 +2877,22 @@ create table b (
create view test.v1 as table test.a;

create view test.v2 as table test.b;

-- issue https://github.com/PostgREST/postgrest/issues/2458
create table with_pk1 (pk1 int primary key);
create table with_pk2 (pk2 int primary key);

create view with_multiple_pks as
select * from with_pk1, with_pk2;

create function with_multiple_pks_insert() returns trigger
language plpgsql as $$
begin
insert into with_pk1 values (new.pk1) on conflict do nothing;
insert into with_pk2 values (new.pk2) on conflict do nothing;
return new;
end
$$;

create trigger ins instead of insert on with_multiple_pks
for each row execute procedure with_multiple_pks_insert();

0 comments on commit 6fe1617

Please sign in to comment.