Skip to content

Struct representing return type of a select query not generated #2127

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

Closed
Archisman-Mridha opened this issue Mar 3, 2023 · 2 comments
Closed

Comments

@Archisman-Mridha
Copy link

Archisman-Mridha commented Mar 3, 2023

Version

1.16.0

What happened?

I have this postgresql query -

SELECT (email, username) FROM users
    WHERE users.email= @email OR users.username= @username
        LIMIT 1;

and I am trying to generate GoLang code.

sqlc doesn't generate a struct representing (email, username). Instead, it just creates interface{ }.

Relevant log output

No response

Database schema

CREATE TABLE users (
    id SERIAL PRIMARY KEY,

    email VARCHAR(255) NOT NULL UNIQUE,
    username VARCHAR(50) NOT NULL UNIQUE,
    password VARCHAR(50) NOT NULL
);

SQL queries

SELECT (email, username) FROM users
    WHERE users.email= @email OR users.username= @username
        LIMIT 1;

Configuration

version: "2"

sql:
    - engine: postgresql
      queries: ./sql/queries.sql
      schema: ./sql/schema.sql
      gen:
        go:
            package: sqlc
            out: ./generated/sqlc
            emit_interface: true

Playground URL

https://play.sqlc.dev/p/0210bd42a17a0804f628d75bd58e69757e9be888a4f9ed713f8acaaa1cbd49d3

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@Archisman-Mridha Archisman-Mridha added bug Something isn't working triage New issues that hasn't been reviewed labels Mar 3, 2023
@Jille
Copy link
Contributor

Jille commented Apr 3, 2023

As a workaround, you can remove the parentheses from your SELECT clause and sqlc will by default generate a struct (because it's more than 1 field returned).

@kyleconroy kyleconroy added 📚 postgresql 🔧 golang 💻 linux and removed triage New issues that hasn't been reviewed labels Jun 7, 2023
@kyleconroy
Copy link
Collaborator

The interface{} is generated because using () return a row type. As Jille mentioned, you'll want to remove the parens. We're tracking generating valid code for these cases in #2760

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

3 participants