I am using sqlc with a PostgreSQL database in a Golang project. My database has the following simplified schema:
CREATE TABLE users (
id SERIAL NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
image TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);
CREATE TABLE posts (
id SERIAL NOT NULL PRIMARY KEY,
title TEXT NOT NULL,
content JSONB,
subreddit_id INT NOT NULL REFERENCES subreddits(id) ON DELETE CASCADE,
author_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);
CREATE TABLE votes (
id SERIAL NOT NULL PRIMARY KEY,
post_id INT NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
user_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
type vote_type NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
UNIQUE(user_id, post_id)
);
This is the query that i have written so sqlc can generate code from it:
-- name: FindPostWithAuthorAndVotes :one
SELECT posts.*, sqlc.embed(users), sqlc.embed(votes)
FROM posts
INNER JOIN users ON users.id = posts.author_id
INNER JOIN votes ON votes.post_id = posts.id
WHERE posts.id = $1;
what i expect to have in the generated type in go code is:
type FindPostWithAuthorAndVotesRow struct {
ID int32 `json:"id"`
Title string `json:"title"`
Content sql.NullRawMessage `json:"content"`
SubredditID int32 `json:"subredditId"`
AuthorID int32 `json:"authorId"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
User User `json:"user"`
Vote []Vote `json:"vote"`
}
but what i get is:
type FindPostWithAuthorAndVotesRow struct {
ID int32 `json:"id"`
Title string `json:"title"`
Content types.NullRawMessage `json:"content"`
SubredditID int32 `json:"subredditId"`
AuthorID int32 `json:"authorId"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
User User `json:"user"`
Vote Vote `json:"vote"`
}
i want the Vote field's type to be a slice of Vote. but i get it as just Vote struct. using sqlc.embed(votes)
is not working. is there a way to make it work?