Skip to content

Complex select produces not enough args to execute query #4029

@dresswithpockets

Description

@dresswithpockets

Version

1.29.0

What happened?

I have two placeholders in the relevant queries. I call the queries like this:

	recentUserAchievements, err := Queries.GetUserRecentAchievements(c.Context(), query.GetUserRecentAchievementsParams{
		UserSlug: userSlug,
		Limit:    20,
	})

I get the error shown in log output when executing this query. Both affected queries produce the same error.

Relevant log output

2025/07/23 16:27:14.613353 users.go:25: [Error] not enough args to execute query: want 3 got 2

Database schema

create table if not exists user
(
    id         integer primary key,
    created_at datetime not null default (datetime('now')),
    updated_at datetime not null,
    deleted_at datetime,
    slug       text     not null
);

create unique index user_unique_slug on user(slug);

create table if not exists user_display_name
(
    id           integer primary key,
    created_at   datetime not null default (datetime('now')),
    deleted_at   datetime,
    user_id      integer not null references user (id),
    display_name text     not null
);

create table if not exists developer
(
    id         integer primary key,
    created_at datetime not null default (datetime('now')),
    updated_at datetime not null,
    deleted_at datetime,
    slug       text     not null
);

create unique index developer_unique_slug on developer(slug);

create table if not exists game
(
    id           integer primary key,
    created_at   datetime not null default (datetime('now')),
    updated_at   datetime not null,
    deleted_at   datetime,
    developer_id integer not null references developer (id),
    slug         text     not null
);

create unique index game_unique_slug_per_dev on game(developer_id, slug);

create table if not exists achievement
(
    id                   integer primary key,
    created_at           datetime not null default (datetime('now')),
    updated_at           datetime not null,
    deleted_at           datetime,
    game_id              integer not null references game (id),
    slug                 text     not null,
    name                 text     not null,
    description          text     not null,
    progress_requirement integer  not null
);

create unique index achievement_unique_slug_per_game on achievement(game_id, slug);

create table if not exists achievement_progress
(
    created_at     datetime not null default (datetime('now')),
    updated_at     datetime not null,
    deleted_at     datetime,
    user_id        integer not null references user (id),
    achievement_id integer not null references achievement (id),
    progress       integer  not null,

    primary key (user_id, achievement_id)
);

SQL queries

-- name: GetUserRecentAchievements :many
select d.slug as developer_slug, g.slug as game_slug, '' as game_name, a.slug as slug, a.name as name, a.description as description
from achievement_progress ap
     join achievement a on ap.achievement_id = a.id
     join user u on ap.user_id = u.id
     join game g on a.game_id = g.id
     join developer d on g.developer_id = d.id
where u.slug = sqlc.arg(user_slug)
  and ap.progress >= a.progress_requirement
  and u.deleted_at is null
  and g.deleted_at is null
  and a.deleted_at is null
  and ap.deleted_at is null
order by ap.created_at desc
limit ?;

-- name: GetOtherUserRecentAchievements :many
select d.slug as developer_slug, g.slug as game_slug, '' as game_name, a.slug as slug, a.name as name, a.description as description, u.slug as user_slug, udn1.display_name as user_display_name
from achievement_progress ap
     join achievement a on ap.achievement_id = a.id
     join user u on ap.user_id = u.id
     join game g on a.game_id = g.id
     join developer d on g.developer_id = d.id
     left outer join user_display_name udn1 on u.id = udn1.user_id and udn1.deleted_at is null
     left outer join user_display_name udn2 on u.id = udn2.user_id and udn2.deleted_at is null and
                                               (udn1.created_at < udn2.created_at or
                                                (udn1.created_at = udn2.created_at and udn1.id < udn2.id))
where u.slug != sqlc.arg(excluded_user_slug)
  and ap.progress >= a.progress_requirement
  and u.deleted_at is null
  and g.deleted_at is null
  and a.deleted_at is null
  and ap.deleted_at is null
order by ap.created_at desc
limit ?;

Configuration

version: "2"
sql:
  - engine: "sqlite"
    queries: "queries"
    schema: "migrations"
    gen:
      go:
        package: "query"
        out: "query"

Playground URL

https://play.sqlc.dev/p/01d4d0d0db2f29ee89bf5d78c97ab679582286e5f68283a3a981bfa9829beeef

What operating system are you using?

Linux

What database engines are you using?

SQLite

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