diff --git a/CHANGELOG.md b/CHANGELOG.md index fe5b3c5616..fa672f0e9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/PostgREST/SchemaCache.hs b/src/PostgREST/SchemaCache.hs index 16341facf8..e67f1d6e22 100644 --- a/src/PostgREST/SchemaCache.hs +++ b/src/PostgREST/SchemaCache.hs @@ -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 = diff --git a/test/spec/Feature/Query/InsertSpec.hs b/test/spec/Feature/Query/InsertSpec.hs index 970cea8910..3392a13d02 100644 --- a/test/spec/Feature/Query/InsertSpec.hs +++ b/test/spec/Feature/Query/InsertSpec.hs @@ -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}|] @@ -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")] @@ -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" <:> "*/*" ] } diff --git a/test/spec/fixtures/schema.sql b/test/spec/fixtures/schema.sql index e244454812..1a47915c7b 100644 --- a/test/spec/fixtures/schema.sql +++ b/test/spec/fixtures/schema.sql @@ -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();