Skip to content
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

Generated query will return row struct instead of table model struct selecting columns out of defined order. #3328

Open
meblum opened this issue Apr 11, 2024 · 0 comments
Labels
bug Something isn't working triage New issues that hasn't been reviewed

Comments

@meblum
Copy link
Contributor

meblum commented Apr 11, 2024

Version

1.26.0

What happened?

When a query specifies all columns, the generated function should return the struct from models.go. However, if the column order from the query is not identical to the column order in the table definition, the generated function will instead return a *Row struct.

Relevant log output

// generated code, note it does not return our Test model struct

type TestGetRow struct {
	Name string
	ID   int32
}

func (q *Queries) TestGet(ctx context.Context, id int32) (TestGetRow, error) {
	row := q.db.QueryRowContext(ctx, testGet, id)
	var i TestGetRow
	err := row.Scan(&i.Name, &i.ID)
	return i, err
}

Database schema

CREATE TABLE IF NOT EXISTS test (
    id INTEGER NOT NULL PRIMARY KEY,
    name TEXT NOT NULL
);

SQL queries

-- name: TestGet :one
SELECT name, id FROM test WHERE id = $1;

Configuration

version: "2"
sql:
  - engine: "postgresql"
    queries: "query.sql"
    schema: "schema.sql"
    gen:
      go:
        out: "db"

Playground URL

https://play.sqlc.dev/p/b9e31f8d197736d3d2bfac8395a0236bce0a7f6bac3b6e3ddfbe54875eabda31

What operating system are you using?

Windows

What database engines are you using?

MySQL, PostgreSQL, SQLite

What type of code are you generating?

Go

@meblum meblum added bug Something isn't working triage New issues that hasn't been reviewed labels Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage New issues that hasn't been reviewed
Projects
None yet
Development

No branches or pull requests

1 participant