Skip to content

sqlc generates the limit and offset as string when using mysql #46

Open
@daison12006013

Description

@daison12006013

Am I the only one encountering this kind of generated query? I've tried using this query, but it doesn't work because it throws an error requiring the type to be numeric.

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''100' offset '0'

Query:

-- name: AcmeGetThreadsIDsByUpdatedAtLimitOffsetQuery :many
SELECT id FROM threads WHERE updated_at BETWEEN sqlc.arg('min_updated_at') AND sqlc.arg('max_updated_at')
order by id asc
limit ? offset ?;

Generated:

export const acmeGetThreadsIDsByUpdatedAtLimitOffsetQueryQuery = `-- name: AcmeGetThreadsIDsByUpdatedAtLimitOffsetQuery :many
SELECT id FROM threads WHERE updated_at BETWEEN ? AND ?
order by id asc
limit ? offset ?`;

export interface AcmeGetThreadsIDsByUpdatedAtLimitOffsetQueryArgs {
    minUpdatedAt: Date;
    maxUpdatedAt: Date;
    limit: string;
    offset: string;
}

export interface AcmeGetThreadsIDsByUpdatedAtLimitOffsetQueryRow {
    id: number;
}

export async function acmeGetThreadsIDsByUpdatedAtLimitOffsetQuery(client: Client, args: AcmeGetThreadsIDsByUpdatedAtLimitOffsetQueryArgs): Promise<AcmeGetThreadsIDsByUpdatedAtLimitOffsetQueryRow[]> {
    const [rows] = await client.query<RowDataPacket[]>({
        sql: seekingGetThreadsIDsByUpdatedAtLimitOffsetQueryQuery,
        values: [args.minUpdatedAt, args.maxUpdatedAt, args.limit, args.offset],
        rowsAsArray: true
    });
    return rows.map(row => {
        return {
            id: row[0]
        };
    });
}

Interim Solution:

By forcing my input limit from being a number then casting it as unknown then string, just faking it to be string!

acmeGetThreadsIDsByUpdatedAtLimitOffsetQuery(client, {
   // ...,
   limit: limit as unknown as string,
   offset: offset as unknown as string,
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions