From 7e55057118c5d4d91521f6b78aa00da066a5e53e Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Tue, 4 Jul 2023 22:26:56 +0300 Subject: [PATCH] Add example for fetching a single column with sqlQuery --- Guide/database.markdown | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Guide/database.markdown b/Guide/database.markdown index 44a7c5b3e..efd642c57 100644 --- a/Guide/database.markdown +++ b/Guide/database.markdown @@ -320,6 +320,19 @@ do result :: [Project] <- sqlQuery "SELECT * FROM ?" [PG.Identifier table] ``` +If you need to fetch only a single column, for example only the ID of a record, you need to help the compiler and type hint +the result, with an `Only` prefix. Here's an example of fetching only the IDs of a `project` table, and converting them to +`Id Project` + +```haskell +allProjectUuids :: [Only UUID] <- sqlQuery "SELECT projects.id FROM projects" () + +let projectIds = + allProjectUuids + -- Extract the UUIDs, and convert to an ID. + |> map (\(Only uuid) -> Id uuid :: Id Project) +``` + ### Scalar Results The [`sqlQuery`](https://ihp.digitallyinduced.com/api-docs/IHP-ModelSupport.html#v:sqlQuery) function always returns a list of rows as the result. When the result of your query is a single value (such as an integer or string) use [`sqlQueryScalar`](https://ihp.digitallyinduced.com/api-docs/IHP-ModelSupport.html#v:sqlQueryScalar):