Skip to content

:copyfrom generates incorrect column name when inserting primary keys #2133

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
twpayne opened this issue Mar 8, 2023 · 2 comments
Closed
Labels
bug Something isn't working triage New issues that hasn't been reviewed

Comments

@twpayne
Copy link
Contributor

twpayne commented Mar 8, 2023

Version

1.17.2

What happened?

I want to use :copyfrom to insert primary key values. sqlc generates an incorrect column name for the primary key field, breaking the query. I made a minimal example to reproduce this, which, when generating code to populate author.author_id results in the error:

ERROR: column "authorID" does not exist (SQLSTATE 42703)

i.e. the author_id column was incorrectly renamed to authorID.

The specific incorrect code is at line 41 in the generated example/copyfrom.go:

func (q *Queries) CreateAuthors(ctx context.Context, authorID []int32) (int64, error) {
	return q.db.CopyFrom(ctx, []string{"authors"}, []string{"authorID"}, &iteratorForCreateAuthors{rows: authorID})
}

Here the incorrect column is visible in the third argument to q.db.CopyFrom.

Relevant log output

No response

Database schema

CREATE TABLE authors (
    author_id SERIAL PRIMARY KEY
);

SQL queries

-- name: CreateAuthors :copyfrom
INSERT INTO authors (author_id) VALUES ($1);

Configuration

version: '2'
sql:
- schema:
  - 'schema.sql'
  queries:
  - 'queries.sql'
  engine: 'postgresql'
  gen:
    go:
      out: 'example'
      sql_package: 'pgx/v4'

Playground URL

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

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@twpayne twpayne added bug Something isn't working triage New issues that hasn't been reviewed labels Mar 8, 2023
@Jille
Copy link
Contributor

Jille commented Apr 3, 2023

This happens because there is only a single column (not necessarily the primary key).

https://github.com/kyleconroy/sqlc/blob/d4902a6021731b34a63e386d1dbd7883f7d8314f/internal/codegen/golang/query.go#L117-L126

v.Name is the Go name, not the DB name. The code just below (for multiple fields) uses f.DBName correctly.

@twpayne
Copy link
Contributor Author

twpayne commented Apr 3, 2023

Thank you very much for the fix :)

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

3 participants