-
Couldn't load subscription status.
- Fork 958
Is it possible to embed structs on a complex query? #3770
Unanswered
stargeneration-winston
asked this question in
Q&A
-
The given example on embedding structs is as follows:
We want to select the student record and the scores they got on a test. Here’s how we’d usually do that:
-- 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:
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.
-- 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
}
If I wish to embed structs on a more complex query, is it possible? For example:
WITH students AS (
SELECT s.*
FROM users s
WHERE s.id = @id
), ...
SELECT
s.*,
...
...
FROM students
WHERE s.id = ...
I tried to replace each s.* with sqlc.embed(users) but then it fails on the s.id WHERE condition.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment