Skip to content
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

v10: Wrong query on M2M relationship when using RPC #2565

Closed
steve-chavez opened this issue Nov 15, 2022 · 1 comment · Fixed by #2568
Closed

v10: Wrong query on M2M relationship when using RPC #2565

steve-chavez opened this issue Nov 15, 2022 · 1 comment · Fixed by #2568
Assignees
Labels
bug embedding resource embedding

Comments

@steve-chavez
Copy link
Member

CREATE TABLE test.groups (
    name text PRIMARY KEY
);

CREATE TABLE test.yards (
    id bigint PRIMARY KEY
);

CREATE TABLE test.group_yard (
    id bigint NOT NULL,
    group_id text NOT NULL REFERENCES test.groups(name),
    yard_id bigint NOT NULL REFERENCES test.yards(id),
    PRIMARY KEY (id, group_id, yard_id)
);

CREATE FUNCTION test.get_yards() RETURNS SETOF test.yards
LANGUAGE sql
AS $$
  select * from test.yards;
$$;
$ curl "localhost:3000/rpc/get_yards?select=groups(*)"

{"code":"42703","details":null,"hint":null,"message":"column get_yards.name does not exist"}

# this works
$ curl "localhost:3000/rpc/get_yards?select=*,groups(*)"
[]

Doesn't happen on v9.0.0 or v9.0.1.

@wolfgangwalther wolfgangwalther added the embedding resource embedding label Nov 15, 2022
@wolfgangwalther
Copy link
Member

I bisected... and no surprise, it's d271942 again ;)

The bad query looks like this:

with pgrst_source as (
select
    "get_yards"."id",
    "get_yards"."name"
from
    "test"."get_yards"()
)
select
    null::bigint as total_result_set,
    pg_catalog.count(_postgrest_t) as page_total,
    coalesce(json_agg(_postgrest_t), '[]')::character varying as body,
    nullif(current_setting('response.headers', true), '') as response_headers,
    nullif(current_setting('response.status', true), '') as response_status
from
    (
    select
        coalesce (
    (
        select
            json_agg("pgrst_source" .*)
        from
            (
            select
                "test"."groups" .*
                from "test"."groups",
                "test"."group_yard"
            where
                "pgrst_source"."id" = "test"."group_yard"."yard_id"
                and "test"."groups"."name" = "test"."group_yard"."group_id" ) "pgrst_source" ),
        '[]') as "groups"
        from "pgrst_source" ) _postgrest_t

while the query with select=*,... looks like this:

with pgrst_source as (
select
    "get_yards" .*
from
    "test"."get_yards"())
select
    null::bigint as total_result_set,
    pg_catalog.count(_postgrest_t) as page_total,
    coalesce(json_agg(_postgrest_t), '[]')::character varying as body,
    nullif(current_setting('response.headers', true), '') as response_headers,
    nullif(current_setting('response.status', true), '') as response_status
from
    (
    select
        "yards" .*,
        coalesce( "yards_groups_1"."yards_groups_1", '[]') as "groups"
    from
        "pgrst_source" as "yards"
    left join lateral (
        select
            json_agg("yards_groups_1") as "yards_groups_1"
            from (
            select
                "test"."groups" .*
            from
                "test"."groups",
                "test"."group_yard"
            where
                "test"."group_yard"."group_id" = "test"."groups"."name"
                and "test"."group_yard"."yard_id" = "yards"."id" ) as "yards_groups_1" ) as "yards_groups_1" on
        true ) _postgrest_t

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug embedding resource embedding
Development

Successfully merging a pull request may close this issue.

2 participants