Skip to content

Override type of table unrelated columns #3973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
suniastar opened this issue May 22, 2025 · 3 comments
Open

Override type of table unrelated columns #3973

suniastar opened this issue May 22, 2025 · 3 comments

Comments

@suniastar
Copy link

suniastar commented May 22, 2025

I've searched through your source code and documentation but I cannot find a solution.
I want to select an entry as well as for all entries in another table that are related to it.

A simplified example would like this:

-- schema.sql
CREATE TABLE authors (
    id BIGSERIAL PRIMARY KEY,
    name TEXT NOT NULL
);

CREATE TABLE books (
    id BIGSERIAL PRIMARY KEY,
    author_id BIGINT NOT NULL REFERENCES authors(id),
    title TEXT NOT NULL
);
-- query.sql
-- name: GetAuthorsWithBooks :many
SELECT a.id,
       a.name,
       jsonb_agg(jsonb_build_object('id',
                                    b.id,
                                    'author_id',
                                    b.author_id,
                                    'title',
                                    b.title,)
                ) FILTER (WHERE b.id IS NOT NULL)
           AS books
FROM authors a
         LEFT JOIN books b
                   ON a.id = b.author_id
GROUP BY a.id;

I would like to produce a result model similiar to this example:

type GetAuthorsWithBooks struct {
    ID    int64  `db:"id" json:"id"`
    Name  string `db:"name" json:"name"`
    // I would like to get this generated attribute
    Books []Book `db:"books" json:"books"`
    // But instead i get this
    Books []byte `db:"books" json:"books"`
}

type Book struct {
    ID            int64  `json:"id"`
    AuthorID      int64  `json:"author_id"`
    Title         string `json:"title"`
}

I tried to use overrides to achieve the desired result model and these overrides work when configured by type but not by column name:

version: "2"
sql:
  - schema: "internal/db/schema.sql"
    queries: "internal/db/query.sql"
    engine: "postgresql"
    gen:
      go:
        package: "dbgen"
        out: "internal/db/gen"
        emit_db_tags: true
        emit_json_tags: true
        json_tags_id_uppercase: false
        json_tags_case_style: snake
        overrides:
          - db_type: json # This one works but overrides all json columns to books which interferes with other queries.
            go_type:
              type: "Book"
              slice: true
          - column: "GetAuthorsWithBooks.books" # This config is not documented but I did not know what else to use as the correct table name.
            go_type:
              type: "Book"
              slice: true

It does not work and does not produce any errors or something else (version 1.29). It just gets ignored as far as I can tell.

Does anyone of a way to archive this without using (materialized) views? Just with plain sqlc?
Or if not does anyone know a way to override a type for a column that is not an existing column in an existing table?

I mean when I would run SELECT now()::timestamptz AS current_time what would I have to fill in here:

sql:
  - gen:
      go:
        overrides:
          - column: "what use here?" # I know you would use override by type here but how would I override this column by name?
            go_type:
              import: time
              type: Time
@antst
Copy link

antst commented May 23, 2025

As it happens, right now I struggle with exactly the same problem :)
Although in my case column its defined as "*.column_name"

@suniastar
Copy link
Author

@antst Did it work for you with *.column_name? I tried this as well but seems to do nothing.

@antst
Copy link

antst commented May 23, 2025

Nope, it doesn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants