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?
votes. If there are 10votesrecords that have samepost_id, then you'd get 10 rows back. You must either aggregate them into one row on DB side (with jsonb type maybe?) or do so on Go side.JSON_AGG(votes.*) AS votesalso. that generated ajson.RawMessagetype for theVotesfield in the generated struct. is there no way to get sqlc to generate[]Votetype?json_aggreturns ajson.RawMessage. You would probably need to override this in the config. An example can be seen in the doc.jsonbtype column. That is not the same as querying a json aggregated column whose type is justjson. Anyways I have written a custom transformer for each sql query where I query aggregated columns, which is kind of inefficient with respect to scaling. But anyways thanks for chipping in.