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

A query that contains a slice and a regular parameter generates query code with incorrect placeholder ordinal #3705

Open
jhlgns opened this issue Nov 13, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@jhlgns
Copy link

jhlgns commented Nov 13, 2024

Version

1.27.0

What happened?

Hello, I was just pulling out my non-existent hairs about why the filtering of my blog articles produced article lists that made no sense.
I have this query:

-- name: ListArticles :many
select *
from articles
where
    (category in (sqlc.slice(categories))) and
    (sqlc.arg(include_drafts) = 1 or is_visible = 1)
order by created_at desc;

Which generates this query code:

const listArticles = `-- name: ListArticles :many

select id, created_at, last_edited_at, is_visible, title, category, content, content_type
from articles
where
    (category in (/*SLICE:categories*/?)) and
    (?2 = 1 or is_visible = 1)
order by created_at desc
`

However, the ?2 here means that not the wanted IncludeDrafts parameter of the paramter struct is inserted, but the second element in the parameter array, which is the second element of the categories array.
When I manually replace the ?2 with ?, it works as expected.

One solution could be either to not emit placeholder ordinals or to enumerate the slice placeholders as well.

Relevant log output

No response

Database schema

No response

SQL queries

No response

Configuration

No response

Playground URL

No response

What operating system are you using?

No response

What database engines are you using?

SQLite

What type of code are you generating?

Go

@jhlgns jhlgns added the bug Something isn't working label Nov 13, 2024
@anhtuan23
Copy link

Same problem for me. I have to move the slice parameter to the last position as a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants