Skip to content

Commit 520be45

Browse files
committed
fix: unexpected results when embedding the same table twice
1 parent bf75869 commit 520be45

5 files changed

Lines changed: 43 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file. From versio
1919

2020
- Shutdown should wait for in flight requests by @mkleczek in #4702
2121
- Fix login with uppercase and mixed case role names by @taimoorzaeem in #4678
22+
- Fix unexpected results when embedding and filtering the same table more than once by @laurenceisla in #4075
2223

2324
### Changed
2425

@@ -28,6 +29,8 @@ All notable changes to this project will be documented in this file. From versio
2829
+ Now fails at startup. Prior to this, it failed with `PGRST205` on requests related to these schemas.
2930
- Build a static executable for aarch64-linux by @wolfgangwalther in #4193
3031
- Build the minimal docker image for aarch64-linux by @wolfgangwalther in #4193
32+
- The name of an embedded table can no longer be used in filters if it has an alias by @laurenceisla in #4075
33+
+ e.g. `?select=alias:table(*)&table.id=eq.1` is not possible anymore, use `?select=alias:table(*)&alias.id=eq.1` instead.
3134

3235
## [14.10] - 2026-04-16
3336

src/PostgREST/Plan.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ updateNode f (targetNodeName:remainingPath, a) (Right (Node rootNode forest)) =
996996
updateNode f (remainingPath, a) (Right target)
997997
where
998998
findNode :: Maybe ReadPlanTree
999-
findNode = find (\(Node ReadPlan{relName, relAlias} _) -> relName == targetNodeName || relAlias == Just targetNodeName) forest
999+
findNode = find (\(Node ReadPlan{relName, relAlias} _) -> fromMaybe relName relAlias == targetNodeName) forest
10001000

10011001
mutatePlan :: Mutation -> QualifiedIdentifier -> ApiRequest -> SchemaCache -> ReadPlanTree -> Either Error MutatePlan
10021002
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
@@ -1070,6 +1070,16 @@ spec = do
10701070
{ "id":4,"children":[]}
10711071
]|] { matchHeaders = [matchContentTypeJson] }
10721072

1073+
it "works when embedding the same table more than once" $
1074+
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`
1075+
[json|[
1076+
{
1077+
"name":"Lake",
1078+
"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"}],
1079+
"work_visits":[{"id": 3, "start_time": "2025-01-01T20:00:00", "visit_type": "work"}]
1080+
}
1081+
]|] { matchHeaders = [matchContentTypeJson] }
1082+
10731083
describe "ordering response" $ do
10741084
it "by a column asc" $
10751085
get "/items?id=lte.2&order=id.asc"
@@ -1152,7 +1162,7 @@ spec = do
11521162
{ matchHeaders = [matchContentTypeJson] }
11531163

11541164
it "ordering embeded entities with alias" $
1155-
get "/projects?id=eq.1&select=id, name, the_tasks:tasks(id, name)&tasks.order=name.asc" `shouldRespondWith`
1165+
get "/projects?id=eq.1&select=id, name, the_tasks:tasks(id, name)&the_tasks.order=name.asc" `shouldRespondWith`
11561166
[json|[{"id":1,"name":"Windows 7","the_tasks":[{"id":2,"name":"Code w7"},{"id":1,"name":"Design w7"}]}]|]
11571167
{ matchHeaders = [matchContentTypeJson] }
11581168

test/spec/fixtures/data.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,3 +970,16 @@ VALUES (1, 'stratosphere', 1),
970970
(2, 'ants from up above',2),
971971
(3, 'vespertine',3),
972972
(4, 'contemporary movement', 1);
973+
974+
TRUNCATE TABLE places CASCADE;
975+
INSERT INTO places (name)
976+
VALUES ('Lake'), ('Mountain'), ('Beach');
977+
978+
TRUNCATE TABLE visits CASCADE;
979+
INSERT INTO visits (place_id, start_time, end_time, visit_type)
980+
VALUES (1, '2025-01-01 10:00','2025-01-01 11:00', 'vacation'),
981+
(1, '2025-01-01 15:00','2025-01-01 16:00', 'vacation'),
982+
(1, '2025-01-01 20:00', '2025-01-01 21:00', 'work'),
983+
(2, '2024-11-01 09:00','2024-11-01 10:00', 'vacation'),
984+
(3, '2024-12-02 13:00','2024-12-02 14:00', 'vacation'),
985+
(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
@@ -3855,3 +3855,18 @@ $_$ language sql;
38553855
create or replace function test.get_tiobe_pls() returns setof test.tiobe_pls as $$
38563856
select * from test.tiobe_pls;
38573857
$$ language sql;
3858+
3859+
create type visit_type as enum ('vacation', 'work');
3860+
3861+
create table places (
3862+
id int primary key generated always as identity,
3863+
name text not null
3864+
);
3865+
3866+
create table visits (
3867+
id int primary key generated always as identity,
3868+
place_id int not null references places(id),
3869+
start_time timestamp,
3870+
end_time timestamp,
3871+
visit_type visit_type
3872+
);

0 commit comments

Comments
 (0)