diff --git a/docs/pages/docs/getting-started/advanced.md b/docs/pages/docs/getting-started/advanced.md index 9f114dac..2124809e 100644 --- a/docs/pages/docs/getting-started/advanced.md +++ b/docs/pages/docs/getting-started/advanced.md @@ -180,4 +180,4 @@ log.Printf("post: %+v", post) ## API reference -To explore all query capabilities, check out the [API reference](walkthrough). +To explore all query capabilities, check out the [API reference](../walkthrough). diff --git a/docs/pages/docs/walkthrough/raw.md b/docs/pages/docs/walkthrough/raw.md index 8c1bf264..59e1e3a4 100644 --- a/docs/pages/docs/walkthrough/raw.md +++ b/docs/pages/docs/walkthrough/raw.md @@ -53,15 +53,18 @@ err := client.Prisma.QueryRaw("SELECT * FROM `Post` WHERE id = ? AND title = ?", #### Custom Query +The Prisma client doesn't support aggregations out of the box. But you can do that via a custom query: + ```go -// note the usage of db.RawString, db.RawInt, etc. var res []struct{ PostID db.RawString `json:"post_id"` - Comments db.RawInt `json:"comments"` + Comments db.RawInt `json:"n_comments"` } -err := client.Prisma.QueryRaw("SELECT post_id, count(*) as comments FROM `Comment` GROUP BY post_id").Exec(ctx, &res) +err := client.Prisma.QueryRaw("SELECT post_id, count(*) as n_comments FROM `Comment` GROUP BY post_id").Exec(ctx, &res) ``` +Note that the query uses `db.RawString` etc in the struct definition to maintain compatibility. Note also that the results are an array of structs, not a struct. + #### Operations Use `ExecuteRaw` for operations such as `INSERT`, `UPDATE` or `DELETE`. It will always return a `Result{Count: int}`, which contains the affected rows. @@ -109,7 +112,7 @@ To ensure compatibility with database and go types, you can use raw types. // note the usage of db.RawString, db.RawInt, etc. var res []struct{ ID db.RawString `json:"post_id"` - Published db.RawBoolean `json:"published"` + Comments db.RawInt `json:"comments"` } err := client.Prisma.QueryRaw(`SELECT post_id, count(*) as comments FROM "Comment" GROUP BY post_id`).Exec(ctx, &res) ```