Skip to content

Unexpected extra fields in Params struct when using BETWEEN and joining tables with same column names #4135

@KoheiMatsuno99

Description

@KoheiMatsuno99

Version

1.30.0

What happened?

I'm using MySQL with sqlc and encountered an issue where the generated Params struct includes duplicated parameters when running a query that:

  • Joins multiple tables which have columns with the same name
  • Uses BETWEEN ? AND ? in the WHERE clause

Although there are only two placeholders in the query, sqlc generates duplicated parameter fields in the Params struct.

For reproducibility, I have prepared a minimal example in this repository:
https://github.com/KoheiMatsuno99/sqlc/tree/generated-extra-params-mysql/photo

Note: I have confirmed that this issue does not occur with PostgreSQL. The correct parameters are generated when running the same query on PostgreSQL.

Relevant log output

No compilation error occurs, but the generated Go struct looks like this:


type ListPhotosByAlbumUploadTimeRangeParams struct {
	FromUploadedAt   time.Time
	FromUploadedAt_2 time.Time
	ToUploadedAt     time.Time
	ToUploadedAt_2   time.Time
}


Expected generated Params struct is:


type ListPhotosByAlbumUploadTimeRangeParams struct {
	FromUploadedAt time.Time
	ToUploadedAt   time.Time
}

Database schema

CREATE TABLE photo (
  id          BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  filename    VARCHAR(255) NOT NULL,
  uploaded_at DATETIME NOT NULL,
  width       INT NOT NULL,
  height      INT NOT NULL
) ENGINE=InnoDB;


CREATE TABLE album (
  id          BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  uploaded_at DATETIME NOT NULL,
  description TEXT
) ENGINE=InnoDB;

CREATE TABLE having_photo (
  album_id BIGINT NOT NULL,
  photo_id       BIGINT NOT NULL,
  PRIMARY KEY (album_id, photo_id),
  CONSTRAINT fk_album_album FOREIGN KEY (album_id)
    REFERENCES album(id) ON DELETE CASCADE,
  CONSTRAINT fk_having_photo_photo FOREIGN KEY (photo_id)
    REFERENCES photo(id) ON DELETE CASCADE
) ENGINE=InnoDB;

SQL queries

-- name: ListPhotosByAlbumUploadTimeRange :many
SELECT *
FROM photo
JOIN having_photo ON photo.id = having_photo.photo_id
JOIN album ON having_photo.album_id = album.id
WHERE album.uploaded_at BETWEEN ? AND ?;

Configuration

version: "2"
sql:
  - engine: "mysql"
    queries: "query.sql"
    schema: "schema.sql"
    gen:
      go:
        package: "photo"
        out: "photo"

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions