GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Override query return type · Issue #781 · sqlc-dev/sqlc

Override query return type #781

New issue
Copy link
New issue
Copy link
Open
#4630
Open
Override query return type#781
#4630
Copy link
Labels
enhancementNew feature or requestNew feature or request

Description

@reddec
reddec
opened on Nov 13, 2020
Issue body actions

Let's assume that we have two tables (postgres)

CREATE TABLE foo (
   id BIGSERIAL NOT NULL,
   value TEXT NOT NULL;
);

CREATE TABLE bar (
   id BIGSERIAL NOT NULL,
   value2 TEXT NOT NULL;
);

Problem is that queries

-- name: GetByFooValue :many
SELECT foo.id, bar.id, foo.value FROM foo INNER JOIN bar ON foo.id = bar.id WHERE value = $1

and

-- name: GetByBarValue :many
SELECT foo.id, bar.id, foo.value FROM foo INNER JOIN bar ON foo.id = bar.id WHERE value2 = $1

will generate different response structs (something like GetByBarValueRow and GetByFooValueRow), however by meaning it's the same objects.

I tried to use rename field in configuration but it didn't help. I quickly checked the source code and looks like there is no way to set the output type.

Is it possible to add something like:

  • use rename also for queries types or
  • (IMHO it's better) add an annotation to queries like name: GetByVarValue :type FooBar :many

Thanks!

Reacted by Tomas Basham, bikingcoder, Jan Bader, Christian Chamoun, Ali Anwar, Aleksei Ershov, miguel-flowalytics, Terry Liu, Shubham Jain, Leo and 22 more

Activity

  1. allochi commented on Jan 22, 2023

    @allochi
    allochi
    on Jan 22, 2023
    More actions

    This is exactly what we are looking for in our project, unfortunately it's holding us back from using SQLC, is this in the pipeline?

  2. kyleconroy
    added
    enhancementNew feature or requestNew feature or request
    on Sep 22, 2023
  3. kyleconroy commented on Sep 22, 2023

    @kyleconroy
    kyleconroy
    on Sep 22, 2023
    Collaborator
    More actions

    Also related to #755

  4. kyleconroy
    changed the title [-](Re)Name query return type[/-] [+]Override query return type[/+] on Oct 2, 2023
  5. swetjen commented on Mar 6, 2024

    @swetjen
    swetjen
    on Mar 6, 2024
    More actions

    I'd like to comment I have the same issue.

    In this example, I get back a PostGetManyRow and a PostsGetByTastyRow for two given queries. These structs are identical except in name. And constantly type asserting them at each call site clutters the code and is tedious.

    I thought that maybe the example in embedding structs might solve the issue, but I still get two differently named types for each query.

    Any thoughts on this?

    Perhaps a configuration option to merge identical structs? Or a paramter in the --name: declaration that enables a merge behavior?

    Would the team be open to a PR to fix this?

  6. kahunacohen commented on Jun 26, 2024

    @kahunacohen
    kahunacohen
    on Jun 26, 2024
    More actions

    Is anybody working on this? I might be willing to issue a PR, if someone leads me in the right direction...

  7. phoenisx commented on Oct 26, 2025

    @phoenisx
    phoenisx
    on Oct 26, 2025
    More actions

    Question:
    Why can't we always use the actual model instead of creating separate row structs for each query?

    Go will use the default values when some of the fields are not present, and leave it for the user to decide/manage the conversion of the models -> entity/domain objects

    For e.g.

    If I have user table in db with fields: id, name, email, hash, created_at, and have two different select queries that return different fields from the user, sqlc can still scan the db result into the User struct, and this model struct will then be managed by the user


    I think I'll use jinzhu/copier (to copy row struct into model struct) till this issue gets resolved. This is a great project, and am still learning. If I can contribute somehow, plz do let me know.

  8. LucaCerina commented on Sep 8, 2026

    @LucaCerina
    LucaCerina
    on Sep 8, 2026
    More actions

    +1

    I have a similar issue with two simple queries

    -- name: GetPersons :many
    SELECT person_id, assigned_id, last_name, date_of_birth, sex::TEXT FROM person
    WHERE ehr_or_manual = false;
    
    -- name: GetPerson :one
    SELECT person_id, assigned_id, last_name, date_of_birth, sex::TEXT FROM person
    WHERE person_id = $1;

    Which generates two structs GetPersonRow and GetPersonsRow despite being the same struct.

    There is a mention of this not being an issue in stackoverflow for version 1.17.0. Is that a fluke?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.