fix: Expose PKs from all tables in a view
Fixes a regression in d2719420f4.
Resolves #2458
Signed-off-by: Wolfgang Walther <walther@technowledgy.de>
This commit is contained in:
committed by
Wolfgang Walther
parent
2158f3d039
commit
6fe1617348
@@ -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
|
||||
|
||||
|
||||
@@ -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 =
|
||||
|
||||
@@ -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" <:> "*/*" ]
|
||||
}
|
||||
|
||||
Vendored
+19
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user