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

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

Open
daison12006013 opened this issue Nov 18, 2024 · 1 comment
Open

Comments

@daison12006013
Copy link

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,
});
@yshrsmz
Copy link
Contributor

yshrsmz commented Nov 18, 2024

It seems like the same issue as #24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants