snapshot of https://github.com/sqlc-dev/sqlc.git (main, 2026-09-16; blobs > 131072B omitted)
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
This commit is contained in:
61
docs/howto/embedding.md
Normal file
61
docs/howto/embedding.md
Normal file
@@ -0,0 +1,61 @@
|
||||
# Embedding structs
|
||||
|
||||
Embedding allows you to reuse existing model structs in more queries, resulting
|
||||
in less manual serialization work. First, imagine we have the following schema
|
||||
with students and test scores.
|
||||
|
||||
```sql
|
||||
CREATE TABLE students (
|
||||
id bigserial PRIMARY KEY,
|
||||
name text NOT NULL,
|
||||
age integer NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE test_scores (
|
||||
student_id bigint NOT NULL,
|
||||
score integer NOT NULL,
|
||||
grade text NOT NULL
|
||||
);
|
||||
```
|
||||
|
||||
We want to select the student record and the scores they got on a test.
|
||||
Here's how we'd usually do that:
|
||||
|
||||
```sql
|
||||
-- name: ScoreAndTests :many
|
||||
SELECT students.*, test_scores.*
|
||||
FROM students
|
||||
JOIN test_scores ON test_scores.student_id = students.id
|
||||
WHERE students.id = $1;
|
||||
```
|
||||
|
||||
When using Go, sqlc will produce a struct like this:
|
||||
|
||||
```go
|
||||
type ScoreAndTestsRow struct {
|
||||
ID int64
|
||||
Name string
|
||||
Age int32
|
||||
StudentID int64
|
||||
Score int32
|
||||
Grade string
|
||||
}
|
||||
```
|
||||
|
||||
With embedding, the struct will contain a model for both tables instead of a
|
||||
flattened list of columns.
|
||||
|
||||
```sql
|
||||
-- name: ScoreAndTests :many
|
||||
SELECT sqlc.embed(students), sqlc.embed(test_scores)
|
||||
FROM students
|
||||
JOIN test_scores ON test_scores.student_id = students.id
|
||||
WHERE students.id = $1;
|
||||
```
|
||||
|
||||
```
|
||||
type ScoreAndTestsRow struct {
|
||||
Student Student
|
||||
TestScore TestScore
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user