Skip to content

Commit

Permalink
Merge pull request #3831 from mathesar-foundation/bugfix_self_ref_fk_…
Browse files Browse the repository at this point in the history
…list

Bugfix listing records from a table with self-referential fk
  • Loading branch information
seancolsen authored Sep 12, 2024
2 parents 9b10aba + 6022a4b commit ad8390c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
4 changes: 2 additions & 2 deletions db/sql/00_msar.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4488,7 +4488,7 @@ SELECT 'jsonb_build_object(' || string_agg(
$j$
%1$L, jsonb_object_agg(
summary_cte_%1$s.fkey, summary_cte_%1$s.summary
)
) FILTER (WHERE summary_cte_%1$s.fkey IS NOT NULL)
$j$,
conkey
), ', '
Expand All @@ -4501,7 +4501,7 @@ CREATE OR REPLACE FUNCTION
msar.build_self_summary_json_expr(tab_id oid) RETURNS TEXT AS $$/*
*/
SELECT CASE WHEN quote_ident(msar.get_selectable_pkey_attnum(tab_id)::text) IS NOT NULL THEN
'jsonb_object_agg(summary_cte_self.key, summary_cte_self.summary)'
'jsonb_object_agg(summary_cte_self.key, summary_cte_self.summary) FILTER (WHERE summary_cte_self.key IS NOT NULL)'
END;
$$ LANGUAGE SQL STABLE RETURNS NULL ON NULL INPUT;

Expand Down
65 changes: 65 additions & 0 deletions db/sql/test_00_msar.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3064,6 +3064,26 @@ END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION __setup_table_with_self_referential_fk() RETURNS SETOF TEXT AS $$
BEGIN
CREATE TABLE categories (
id serial primary key,
name TEXT,
parent INT REFERENCES categories(id)
);
INSERT INTO categories (id, parent, name) VALUES
( 1, NULL, 'Tools'),
( 2, 1 , 'Power tools'),
( 3, 1 , 'Hand tools'),
( 4, 2 , 'Drills'),
( 5, 3 , 'Screwdrivers'),
( 6, 3 , 'Wrenches');
-- Reset sequence:
PERFORM setval('categories_id_seq', (SELECT max(id) FROM categories));
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION test_list_records_from_table() RETURNS SETOF TEXT AS $$
DECLARE
rel_id oid;
Expand Down Expand Up @@ -3401,6 +3421,51 @@ END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION test_list_records_for_table_with_self_referential_fk() RETURNS SETOF TEXT AS $$
DECLARE
rel_id oid;
BEGIN
PERFORM __setup_table_with_self_referential_fk();
rel_id := 'categories'::regclass::oid;
RETURN NEXT is(
msar.list_records_from_table(
tab_id => rel_id,
limit_ => 10,
offset_ => null,
order_ => null,
filter_ => null,
group_ => null
),
$j${
"count": 6,
"results": [
{"1": 1, "2": "Tools", "3": null},
{"1": 2, "2": "Power tools", "3": 1},
{"1": 3, "2": "Hand tools", "3": 1},
{"1": 4, "2": "Drills", "3": 2},
{"1": 5, "2": "Screwdrivers", "3": 3},
{"1": 6, "2": "Wrenches", "3": 3}
],
"grouping": null,
"record_summaries": null,
"linked_record_summaries": {
"3": {
"1": "Tools",
"2": "Power tools",
"3": "Hand tools"
}
}
}$j$ || jsonb_build_object(
'query', concat(
'SELECT msar.format_data(id) AS "1", msar.format_data(name) AS "2",'
' msar.format_data(parent) AS "3" FROM public.categories ORDER BY "1" ASC LIMIT ''10'' OFFSET NULL'
)
)
);
END;
$$ LANGUAGE plpgsql;


-- msar.build_order_by_expr ------------------------------------------------------------------------

CREATE OR REPLACE FUNCTION test_build_order_by_expr() RETURNS SETOF TEXT AS $$
Expand Down

0 comments on commit ad8390c

Please sign in to comment.