Skip to content

Commit 10bcc5e

Browse files
committed
fix: unexpected results when embedding the same table twice
1 parent 52cf465 commit 10bcc5e

4 files changed

Lines changed: 42 additions & 4 deletions

File tree

src/PostgREST/Plan.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ updateNode f (targetNodeName:remainingPath, a) (Right (Node rootNode forest)) =
968968
updateNode f (remainingPath, a) (Right target)
969969
where
970970
findNode :: Maybe ReadPlanTree
971-
findNode = find (\(Node ReadPlan{relName, relAlias} _) -> relName == targetNodeName || relAlias == Just targetNodeName) forest
971+
findNode = find (\(Node ReadPlan{relName, relAlias} _) -> fromMaybe relName relAlias == targetNodeName) forest
972972

973973
mutatePlan :: Mutation -> QualifiedIdentifier -> ApiRequest -> SchemaCache -> ReadPlanTree -> Either Error MutatePlan
974974
mutatePlan mutation qi ApiRequest{iPreferences=Preferences{..}, ..} SchemaCache{dbTables, dbRepresentations} readReq =

test/spec/Feature/Query/QuerySpec.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,16 @@ spec = do
10561056
{ "id":4,"children":[]}
10571057
]|] { matchHeaders = [matchContentTypeJson] }
10581058

1059+
it "works when embedding the same table more than once" $
1060+
get "/places?select=name,visits(id,start_time,visit_type),work_visits:visits(id,start_time,visit_type)&id=eq.1&visits.visit_type=neq.work&visits.start_time=gt.20250101+00:00&work_visits.visit_type=eq.work&work_visits.start_time=gt.20250101+00:00" `shouldRespondWith`
1061+
[json|[
1062+
{
1063+
"name":"Lake",
1064+
"visits":[{"id": 1, "start_time": "2025-01-01T10:00:00", "visit_type": "vacation"}, {"id": 2, "start_time": "2025-01-01T15:00:00", "visit_type": "vacation"}],
1065+
"work_visits":[{"id": 3, "start_time": "2025-01-01T20:00:00", "visit_type": "work"}]
1066+
}
1067+
]|] { matchHeaders = [matchContentTypeJson] }
1068+
10591069
describe "ordering response" $ do
10601070
it "by a column asc" $
10611071
get "/items?id=lte.2&order=id.asc"
@@ -1138,7 +1148,7 @@ spec = do
11381148
{ matchHeaders = [matchContentTypeJson] }
11391149

11401150
it "ordering embeded entities with alias" $
1141-
get "/projects?id=eq.1&select=id, name, the_tasks:tasks(id, name)&tasks.order=name.asc" `shouldRespondWith`
1151+
get "/projects?id=eq.1&select=id, name, the_tasks:tasks(id, name)&the_tasks.order=name.asc" `shouldRespondWith`
11421152
[json|[{"id":1,"name":"Windows 7","the_tasks":[{"id":2,"name":"Code w7"},{"id":1,"name":"Design w7"}]}]|]
11431153
{ matchHeaders = [matchContentTypeJson] }
11441154

test/spec/fixtures/data.sql

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,8 +961,21 @@ INSERT INTO artists
961961
VALUES (1, 'duster'), (2, 'black country, new road'), (3, 'bjork');
962962

963963
TRUNCATE TABLE albums CASCADE;
964-
INSERT INTO albums
964+
INSERT INTO albums
965965
VALUES (1, 'stratosphere', 1),
966-
(2, 'ants from up above',2),
966+
(2, 'ants from up above',2),
967967
(3, 'vespertine',3),
968968
(4, 'contemporary movement', 1);
969+
970+
TRUNCATE TABLE places CASCADE;
971+
INSERT INTO places (name)
972+
VALUES ('Lake'), ('Mountain'), ('Beach');
973+
974+
TRUNCATE TABLE visits CASCADE;
975+
INSERT INTO visits (place_id, start_time, end_time, visit_type)
976+
VALUES (1, '2025-01-01 10:00','2025-01-01 11:00', 'vacation'),
977+
(1, '2025-01-01 15:00','2025-01-01 16:00', 'vacation'),
978+
(1, '2025-01-01 20:00', '2025-01-01 21:00', 'work'),
979+
(2, '2024-11-01 09:00','2024-11-01 10:00', 'vacation'),
980+
(3, '2024-12-02 13:00','2024-12-02 14:00', 'vacation'),
981+
(1, '2023-01-02 20:00','2023-01-01 21:00', 'work');

test/spec/fixtures/schema.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3820,3 +3820,18 @@ comment on function test.collision_test_func(id integer) is 'fizzbuzz';
38203820
create or replace function test.delete_all_items() returns setof items as $$
38213821
delete from items where id <= 15 returning *; -- deletes 15 items, then return them
38223822
$$ language sql;
3823+
3824+
create type visit_type as enum ('vacation', 'work');
3825+
3826+
create table places (
3827+
id int primary key generated always as identity,
3828+
name text not null
3829+
);
3830+
3831+
create table visits (
3832+
id int primary key generated always as identity,
3833+
place_id int not null references places(id),
3834+
start_time timestamp,
3835+
end_time timestamp,
3836+
visit_type visit_type
3837+
);

0 commit comments

Comments
 (0)