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

Embedding a struct in a has-one relationship #3746

Unanswered
bjubes asked this question in Issue Triage
Discussion options

In my schema i have a Job that always has a foreign key to a video. I'd like the Job struct to have an embedded video every time i am using it. I can achieve this on a select doing the following:

-- name: GetJobEmbedVideo :one
SELECT sqlc.embed(videos), job.*
FROM job
JOIN videos ON job.video_id= videos.id
WHERE job.id = 1ドル;

output struct:

type GetJobEmbedVideoRow struct {
	Video Video `json:"video"`
	ID int32 `json:"id"`
	Action string `json:"action"`
	VideoID uuid.UUID `json:"video_id"`
	Completed bool `json:"completed"`
	Errored bool `json:"errored"`
	Attempts int32 `json:"attempts"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`

but I can't seem to get it to work when inserting a job. I've tried

INSERT INTO job(
 action, video_id
) VALUES (
 1,ドル 2ドル
)
RETURNING sqlc.embed(videos), job.*;

which won't compile.

INSERT INTO job(
 action, video_id
) VALUES (
 1,ドル 2ドル
)
RETURNING (
 SELECT sqlc.embed(videos), job.*
 FROM job
 JOIN videos ON job.video_id = videos.id
 WHERE job.id = lastval()
);

gives:

type CreateJobRow struct {
	Video Video `json:"video"`
}

What is the correct SQL for this to work on insert (and hopefully update as well)

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
Labels
None yet
1 participant

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