sqlc
sqlc copied to clipboard
Extra argument added to COALESCE in generated SQLite query causes missing argument error
Version
1.26.0
What happened?
I'm using SQLite to create the following query:
-- name: GetAllPaginatedForumPosts :many
SELECT p.id, p.title, p.content, p.created_at, p.updated_at, p.likes
FROM post p
WHERE p.id < COALESCE(sqlc.narg('last_id'), (SELECT MAX(id) FROM post) + 1)
ORDER BY p.id DESC
LIMIT ?;
The generated output adds an extra 2 after the first COALESCE argument in the WHERE clause:
const getAllPaginatedForumPosts = `-- name: GetAllPaginatedForumPosts :many
SELECT p.id, p.title, p.content, p.user_id, p.channel_id, p.created_at, p.updated_at, p.likes,
u.username, c.name as channel_name
FROM post p
JOIN user_account u ON p.user_id = u.id
JOIN channel c ON p.channel_id = c.id
WHERE p.id < COALESCE(?2, (SELECT MAX(id) FROM post) + 1)
ORDER BY p.id DESC
LIMIT ?
`
This generates the following error: missing argument with index 3. If I manually remove the number 2, the query goes through.
Relevant log output
`missing argument with index 3`
Database schema
CREATE TABLE post (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
likes TEXT DEFAULT '{}'
);
SQL queries
-- name: GetAllPaginatedForumPosts :many
SELECT p.id, p.title, p.content, p.created_at, p.updated_at, p.likes
FROM post p
WHERE p.id < COALESCE(sqlc.narg('last_id'), (SELECT MAX(id) FROM post) + 1)
ORDER BY p.id DESC
LIMIT ?;
Configuration
version: "2"
sql:
- engine: "sqlite"
queries: "sql/queries"
schema: "sql/schemas"
gen:
go:
out: "internal/generated/database"
Playground URL
https://play.sqlc.dev/p/0a1a38acf75b6e96542e9c37d008453d0d3991a42dd43ef67c4961c1214a1843
What operating system are you using?
macOS
What database engines are you using?
SQLite
What type of code are you generating?
Go
I have the same issue (my query is structurally similar). It works fine if you use named parameters for all your params (see playground) but I can't tell if that's a known limitation or a feature. Either way, loving sqlc!