-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: WIP allow spread operators in to-many relationships #3640
base: main
Are you sure you want to change the base?
Conversation
1a02416
to
dd89c12
Compare
My approach right now is to generate this query for a to-many request: curl 'localhost:3000/clients?select=name,...projects(name,id)' SELECT "test"."clients"."name",
"clients_projects_1"."name",
"clients_projects_1"."id"
FROM "test"."clients"
LEFT JOIN LATERAL (
SELECT json_agg("projects_1"."name") AS "name",
json_agg("projects_1"."id") AS "id"
FROM "test"."projects" AS "projects_1"
WHERE "projects_1"."client_id" = "test"."clients"."id"
) AS "clients_projects_1" ON TRUE Right now this gives the expected result. But aggregates are not working correctly, because they are designed to be selected in the top query with a SELECT "test"."clients"."name",
json_agg("clients_projects_1"."name") AS "name",
json_agg("clients_projects_1"."id") AS "id"
FROM "test"."clients"
LEFT JOIN LATERAL (
SELECT "projects_1"."name",
"projects_1"."id"
FROM "test"."projects" AS "projects_1"
WHERE "projects_1"."client_id" = "test"."clients"."id"
) AS "clients_projects_1" ON TRUE
GROUP BY "test"."clients"."name" Not sure which one is better/easier right now... I'm thinking the latter. |
ApiRequestErrorCode19 | ||
("A spread operation on '" <> target <> "' is not possible") | ||
(Just $ JSON.String $ "'" <> origin <> "' and '" <> target <> "' do not form a many-to-one or one-to-one relationship") | ||
"Selecting '*' is not allowed in a spread operation for one-to-many or many-to-many relationships" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that, instead of forbidding *
, it would be better to expand the selection using the column names from the Schema Cache. In the same way as aggregates do it here:
postgrest/src/PostgREST/Plan.hs
Lines 410 to 414 in c3070bb
-- | Expand "select *" into explicit field names of the table in the following situations: | |
-- * When there are data representations present. | |
-- * When there is an aggregate function in a given ReadPlan or its parent. | |
expandStars :: ResolverContext -> ReadPlanTree -> Either ApiRequestError ReadPlanTree | |
expandStars ctx rPlanTree = Right $ expandStarsForReadPlan False rPlanTree |
Having the json_agg in the outer query would make the query cleaner, imho. |
WIP on #3041