Skip to content

Override query return type #781

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
reddec opened this issue Nov 13, 2020 · 4 comments
Open

Override query return type #781

reddec opened this issue Nov 13, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@reddec
Copy link
Contributor

reddec commented Nov 13, 2020

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!

@allochi
Copy link

allochi commented Jan 22, 2023

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?

@kyleconroy kyleconroy added the enhancement New feature or request label Sep 22, 2023
@kyleconroy
Copy link
Collaborator

Also related to #755

@kyleconroy kyleconroy changed the title (Re)Name query return type Override query return type Oct 2, 2023
@swetjen
Copy link

swetjen commented Mar 6, 2024

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?

@kahunacohen
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants