fix: Detect all relationships when multiple view columns reference the same table column

Signed-off-by: Wolfgang Walther <walther@technowledgy.de>
This commit is contained in:
Wolfgang Walther
2022-10-28 18:12:58 +02:00
committed by Wolfgang Walther
parent 425ab70ec7
commit 2c31c64325
4 changed files with 187 additions and 20 deletions
@@ -120,6 +120,41 @@ spec =
, matchHeaders = [matchContentTypeJson]
}
it "errs with multiple references to the same composite key columns in a view" $
get "/i2459_composite_v2?select=*,i2459_composite_v1(*)" `shouldRespondWith`
[json|
{
"code": "PGRST201",
"details": [
{
"cardinality": "many-to-one",
"embedding": "i2459_composite_v2 with i2459_composite_v1",
"relationship": "i2459_composite_t2_t1_a_t1_b_fkey using i2459_composite_v2(t1_a1, t1_b1) and i2459_composite_v1(a, b)"
},
{
"cardinality": "many-to-one",
"embedding": "i2459_composite_v2 with i2459_composite_v1",
"relationship": "i2459_composite_t2_t1_a_t1_b_fkey using i2459_composite_v2(t1_a1, t1_b2) and i2459_composite_v1(a, b)"
},
{
"cardinality": "many-to-one",
"embedding": "i2459_composite_v2 with i2459_composite_v1",
"relationship": "i2459_composite_t2_t1_a_t1_b_fkey using i2459_composite_v2(t1_a2, t1_b1) and i2459_composite_v1(a, b)"
},
{
"cardinality": "many-to-one",
"embedding": "i2459_composite_v2 with i2459_composite_v1",
"relationship": "i2459_composite_t2_t1_a_t1_b_fkey using i2459_composite_v2(t1_a2, t1_b2) and i2459_composite_v1(a, b)"
}
],
"hint": "Try changing 'i2459_composite_v1' to one of the following: 'i2459_composite_v1!i2459_composite_t2_t1_a_t1_b_fkey', 'i2459_composite_v1!i2459_composite_t2_t1_a_t1_b_fkey', 'i2459_composite_v1!i2459_composite_t2_t1_a_t1_b_fkey', 'i2459_composite_v1!i2459_composite_t2_t1_a_t1_b_fkey'. Find the desired relationship in the 'details' key.",
"message": "Could not embed because more than one relationship was found for 'i2459_composite_v2' and 'i2459_composite_v1'"
}
|]
{ matchStatus = 300
, matchHeaders = [matchContentTypeJson]
}
context "disambiguating requests with embed hints" $ do
context "using FK to specify the relationship" $ do
@@ -240,6 +275,20 @@ spec =
[json| [ { "name": "site 1", "main_project_id": { "name": "big project 1" } } ] |]
{ matchHeaders = [matchContentTypeJson] }
it "can specify all view column names that reference the same base column" $ do
get "/i2459_simple_v1?select=*,i2459_simple_v2!t1_id1(*)" `shouldRespondWith`
[json| [] |]
{ matchHeaders = [matchContentTypeJson] }
get "/i2459_simple_v1?select=*,i2459_simple_v2!t1_id2(*)" `shouldRespondWith`
[json| [] |]
{ matchHeaders = [matchContentTypeJson] }
get "/i2459_simple_v2?select=*,i2459_simple_v1!t1_id1(*)" `shouldRespondWith`
[json| [] |]
{ matchHeaders = [matchContentTypeJson] }
get "/i2459_simple_v2?select=*,i2459_simple_v1!t1_id2(*)" `shouldRespondWith`
[json| [] |]
{ matchHeaders = [matchContentTypeJson] }
context "using the junction to disambiguate the request" $
it "can specify the junction of an m2m relationship" $ do
get "/sites?select=*,big_projects!jobs(name)&site_id=in.(1,2)" `shouldRespondWith`
@@ -341,6 +390,14 @@ spec =
}
]|] { matchHeaders = [matchContentTypeJson] }
it "can specify all view column names that reference the same base column" $ do
get "/i2459_self_v1?select=*,parent(*),grandparent(*)" `shouldRespondWith`
[json| [] |]
{ matchHeaders = [matchContentTypeJson] }
get "/i2459_self_v2?select=*,parent(*),grandparent(*)" `shouldRespondWith`
[json| [] |]
{ matchHeaders = [matchContentTypeJson] }
context "two self reference foreign keys" $ do
it "embeds parents" $
get "/organizations?select=id,name,referee(id,name),auditor(id,name)&id=eq.3" `shouldRespondWith`
@@ -506,4 +563,3 @@ spec =
}
]|]
{ matchHeaders = [matchContentTypeJson] }
+62
View File
@@ -2918,3 +2918,65 @@ $$ LANGUAGE sql STABLE;
CREATE FUNCTION test.computed_projects(test.clients) RETURNS SETOF test.projects ROWS 1 AS $$
SELECT * FROM test.projects WHERE client_id = $1.id;
$$ LANGUAGE sql STABLE;
-- issue https://github.com/PostgREST/postgrest/issues/2459
create table public.i2459_simple_t1 (
id int primary key
);
create table public.i2459_simple_t2 (
t1_id int references public.i2459_simple_t1
);
create view i2459_simple_v1 as table public.i2459_simple_t1;
create view i2459_simple_v2 as
select t1_id as t1_id1, t1_id as t1_id2 from public.i2459_simple_t2;
create table public.i2459_composite_t1 (
primary key (a,b),
a int,
b int
);
create table public.i2459_composite_t2 (
t1_a int,
t1_b int,
constraint i2459_composite_t2_t1_a_t1_b_fkey foreign key (t1_a, t1_b) references public.i2459_composite_t1
);
create view i2459_composite_v1 as table public.i2459_composite_t1;
create view i2459_composite_v2 as
select t1_a as t1_a1,
t1_b as t1_b1,
t1_a as t1_a2,
t1_b as t1_b2
from public.i2459_composite_t2;
create table public.i2459_self_t (
id int primary key,
parent int references public.i2459_self_t,
type text
);
create view i2459_self_v1 as
select parent.parent as grandparent,
child.parent,
child.id
from public.i2459_self_t as parent
join public.i2459_self_t as child
on child.parent = parent.id
where child.type = 'A';
create view i2459_self_v2 as
select parent.parent as grandparent,
child.parent,
child.id
from public.i2459_self_t as parent
join public.i2459_self_t as child
on child.parent = parent.id
where child.type = 'B';