Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is it possible to embed structs on a complex query? #3770

Unanswered
stargeneration-winston asked this question in Q&A
Discussion options

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.

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /