sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

sqlc.slice() doesn't work with composite keys

Open b3lrog opened this issue 1 month ago • 0 comments

Version

1.30.0

What happened?

The included schema and query generates a function with this signature: func (q *Queries) ListAttachmentsByReplySlice(ctx context.Context) ([]ListAttachmentsByReplySliceRow, error) This is pretty useless as I can't pass in my params. Not tried running it but I assume it either errors or returns a nil slice.

I was expecting sqlc to instead generate a struct like:

type ListAttachmentsByReplySliceParams struct {
	ThreadID  uint32
	ReplyID uint32
}

and a function using that struct like: func (q *Queries) ListAttachmentsByReplySlice(ctx context.Context, replyKeys []ListAttachmentsByReplySliceParams) ([]ListAttachmentsByReplySliceRow, error)

Relevant log output


Database schema

CREATE TABLE `threads` (
  `thread_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` TEXT NOT NULL,
  PRIMARY KEY (`thread_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `replies` (
  `thread_id` INT UNSIGNED NOT NULL,
  `reply_id` INT UNSIGNED NOT NULL,
  `comment` TEXT NOT NULL,
  PRIMARY KEY (`thread_id`,`reply_id`),
  FOREIGN KEY (`thread_id`)
    REFERENCES threads(`thread_id`)
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `attachments` (
  `thread_id` INT UNSIGNED NOT NULL,
  `reply_id` INT UNSIGNED NOT NULL,
  `attachment_id` INT UNSIGNED NOT NULL,
  `file_path` TEXT NOT NULL,
  PRIMARY KEY (`thread_id`,`reply_id`,`attachment_id`),
  FOREIGN KEY (`thread_id`,`reply_id`)
    REFERENCES replies(`thread_id`,`reply_id`)
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

SQL queries

-- name: ListAttachmentsByReplySlice :many
SELECT attachments.*
FROM attachments
WHERE (attachments.thread_id, attachments.reply_id) IN (sqlc.slice(reply_keys))
ORDER BY attachments.thread_id ASC, attachments.reply_id ASC, attachments.attachment_id ASC;

Configuration


Playground URL

No response

What operating system are you using?

Linux

What database engines are you using?

MySQL

What type of code are you generating?

Go

b3lrog avatar Oct 27 '25 11:10 b3lrog