-
-
Notifications
You must be signed in to change notification settings - Fork 96
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
docs(walkthrough): minor clarifications #884
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure about this, would need a mention in the postgres part as well. Maybe I need to refactor this page with some mdx components to have a language switcher in the docs as it's a lot of duplicatoin here, but haven't gotten the chance |
||||||
|
||||||
#### 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"` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
err := client.Prisma.QueryRaw(`SELECT post_id, count(*) as comments FROM "Comment" GROUP BY post_id`).Exec(ctx, &res) | ||||||
``` | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe
comments_count
?