diff --git a/src/PostgREST/PgQuery.hs b/src/PostgREST/PgQuery.hs index 6d678f394..b9dc71210 100644 --- a/src/PostgREST/PgQuery.hs +++ b/src/PostgREST/PgQuery.hs @@ -95,14 +95,14 @@ iffNotT (B.Stmt aq ap apre) (B.Stmt bq bp bpre) = countT :: StatementT countT s = - s { B.stmtTemplate = "WITH qqq AS (" <> B.stmtTemplate s <> ") SELECT count(1) FROM qqq" } + s { B.stmtTemplate = "WITH qqq AS (" <> B.stmtTemplate s <> ") SELECT pg_catalog.count(1) FROM qqq" } countRows :: QualifiedTable -> PStmt -countRows t = B.Stmt ("select count(1) from " <> fromQt t) empty True +countRows t = B.Stmt ("select pg_catalog.count(1) from " <> fromQt t) empty True asJsonWithCount :: StatementT asJsonWithCount s = s { B.stmtTemplate = - "count(t), array_to_json(array_agg(row_to_json(t)))::character varying from (" + "pg_catalog.count(t), array_to_json(array_agg(row_to_json(t)))::character varying from (" <> B.stmtTemplate s <> ") t" } asJsonRow :: StatementT diff --git a/test/Feature/QuerySpec.hs b/test/Feature/QuerySpec.hs index 877b72cb5..7cadff625 100644 --- a/test/Feature/QuerySpec.hs +++ b/test/Feature/QuerySpec.hs @@ -18,6 +18,11 @@ spec = createJsonData) . afterAll_ (clearTable "items" >> clearTable "no_pk" >> clearTable "simple_pk") . around withApp $ do + + describe "Querying a table with a column called count" $ + it "should not confuse count column with pg_catalog.count aggregate" $ + get "/has_count_column" `shouldRespondWith` 200 + describe "Querying a nonexistent table" $ it "causes a 404" $ get "/faketable" `shouldRespondWith` 404 diff --git a/test/Feature/StructureSpec.hs b/test/Feature/StructureSpec.hs index a1b7f19f0..57e03d426 100644 --- a/test/Feature/StructureSpec.hs +++ b/test/Feature/StructureSpec.hs @@ -16,6 +16,7 @@ spec = around withApp $ do `shouldRespondWith` [json| [ {"schema":"1","name":"auto_incrementing_pk","insertable":true} , {"schema":"1","name":"compound_pk","insertable":true} + , {"schema":"1","name":"has_count_column","insertable":false} , {"schema":"1","name":"has_fk","insertable":true} , {"schema":"1","name":"insertable_view_with_join","insertable":true} , {"schema":"1","name":"items","insertable":true} diff --git a/test/fixtures/schema.sql b/test/fixtures/schema.sql index d5a54adff..d3f9dadbb 100755 --- a/test/fixtures/schema.sql +++ b/test/fixtures/schema.sql @@ -182,6 +182,11 @@ CREATE VIEW "1".insertable_view_with_join AS ALTER TABLE "1".insertable_view_with_join OWNER TO postgrest_test; +CREATE VIEW "1".has_count_column AS + SELECT 1 AS count; + +ALTER TABLE "1".insertable_view_with_join OWNER TO postgrest_test; + CREATE TABLE items ( id bigint NOT NULL @@ -563,6 +568,11 @@ REVOKE ALL ON TABLE insertable_view_with_join FROM postgrest_test; GRANT ALL ON TABLE insertable_view_with_join TO postgrest_test; GRANT ALL ON TABLE insertable_view_with_join TO postgrest_anonymous; +REVOKE ALL ON TABLE has_count_column FROM PUBLIC; +REVOKE ALL ON TABLE has_count_column FROM postgrest_test; +GRANT ALL ON TABLE has_count_column TO postgrest_test; +GRANT ALL ON TABLE has_count_column TO postgrest_anonymous; + REVOKE ALL ON FUNCTION public.always_true("1".items) FROM PUBLIC; REVOKE ALL ON FUNCTION public.always_true("1".items) FROM postgrest_test; GRANT ALL ON FUNCTION public.always_true("1".items) TO postgrest_test;