sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
12.48k stars 782 forks source link

copyfrom not respecting `query_parameter_limit` #3388

Open KlemenPl opened 4 months ago

KlemenPl commented 4 months ago

Version

1.26.0

What happened?

When using :copyfrom query with query_parameter_limit set, generated code will still try to use parameter struct:

func (q *Queries) CreateAuthors(ctx context.Context, arg []CreateAuthorsParams) (int64, error) {
    return q.db.CopyFrom(ctx, []string{"author"}, []string{"name", "bio"}, &iteratorForCreateAuthors{rows: arg})
}

Meanwhile, if you use :one it will respect set query_parameter_limit:

func (q *Queries) CreateAuthor(ctx context.Context, name string, bio pgtype.Text) (int64, error) {
    row := q.db.QueryRow(ctx, createAuthor, name, bio)
    var id int64
    err := row.Scan(&id)
    return id, err
}

Relevant log output

No response

Database schema

CREATE TABLE author (
  id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL,
  bio  text
);

SQL queries

-- name: CreateAuthor :one
INSERT INTO author
    (name, bio)
VALUES (@name, @bio)
RETURNING id;

-- name: CreateAuthors :copyfrom
INSERT INTO author
    (name, bio)
VALUES (@name, @bio);

Configuration

{
  "version": "2",
  "sql": [{
    "schema": "schema.sql",
    "queries": "query.sql",
    "engine": "postgresql",
    "gen": {
      "go": {
        "out": "db",
        "query_parameter_limit": 100,
        "sql_package": "pgx/v5"
      }
    }
  }]
}

Playground URL

https://play.sqlc.dev/p/849ecfee7a92c0862b4e877189cb3ddd33c2b181ae1266ada98e8f25f635004f

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

aliml92 commented 4 months ago

I am having the same issue

ysmilda commented 2 months ago

As copyfrom is a function to import multiple rows at once it makes sense that it always takes an slice as input. What would the interface be otherwise?

func (q *Queries) CreateAuthors(ctx context.Context, author1, bio1, author2, bio2) is far from ergonomic to use.

KlemenPl commented 2 months ago

As copyfrom is a function to import multiple rows at once it makes sense that it always takes an slice as input. What would the interface be otherwise?

func (q *Queries) CreateAuthors(ctx context.Context, author1, bio1, author2, bio2) is far from ergonomic to use.

I agree, it should take slice as an input. My problem is that CreateAuthorParams is not generated, if you use query_parameter_limit and therefore the code does not compile (please see playground example).

ysmilda commented 2 months ago

I've run into that as well. It is a known issue (#3197) and already fixed in the main branch (#3446). The problem being that it is not yet released.

You could go install the latest commit and execute the binary from the go/bin folder.