diff --git a/drizzle-orm/src/mysql-core/dialect.ts b/drizzle-orm/src/mysql-core/dialect.ts index 0e2f7380a..4a72d9c5f 100644 --- a/drizzle-orm/src/mysql-core/dialect.ts +++ b/drizzle-orm/src/mysql-core/dialect.ts @@ -326,7 +326,9 @@ export class MySqlDialect { groupBySql = sql` group by ${sql.join(groupBy, sql`, `)}`; } - const limitSql = limit ? sql` limit ${limit}` : undefined; + const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0) + ? sql` limit ${limit}` + : undefined; const offsetSql = offset ? sql` offset ${offset}` : undefined; @@ -403,7 +405,9 @@ export class MySqlDialect { orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)} `; } - const limitSql = limit ? sql` limit ${limit}` : undefined; + const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0) + ? sql` limit ${limit}` + : undefined; const operatorChunk = sql.raw(`${type} ${isAll ? 'all ' : ''}`); diff --git a/drizzle-orm/src/pg-core/dialect.ts b/drizzle-orm/src/pg-core/dialect.ts index 065c49ce8..fff2ce65a 100644 --- a/drizzle-orm/src/pg-core/dialect.ts +++ b/drizzle-orm/src/pg-core/dialect.ts @@ -357,7 +357,9 @@ export class PgDialect { groupBySql = sql` group by ${sql.join(groupBy, sql`, `)}`; } - const limitSql = limit ? sql` limit ${limit}` : undefined; + const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0) + ? sql` limit ${limit}` + : undefined; const offsetSql = offset ? sql` offset ${offset}` : undefined; @@ -443,7 +445,9 @@ export class PgDialect { orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)} `; } - const limitSql = limit ? sql` limit ${limit}` : undefined; + const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0) + ? sql` limit ${limit}` + : undefined; const operatorChunk = sql.raw(`${type} ${isAll ? 'all ' : ''}`); diff --git a/drizzle-orm/src/sqlite-core/dialect.ts b/drizzle-orm/src/sqlite-core/dialect.ts index 50aa30e33..645e15592 100644 --- a/drizzle-orm/src/sqlite-core/dialect.ts +++ b/drizzle-orm/src/sqlite-core/dialect.ts @@ -295,7 +295,9 @@ export abstract class SQLiteDialect { const orderBySql = orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : undefined; - const limitSql = limit ? sql` limit ${limit}` : undefined; + const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0) + ? sql` limit ${limit}` + : undefined; const offsetSql = offset ? sql` offset ${offset}` : undefined; @@ -362,7 +364,9 @@ export abstract class SQLiteDialect { orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)}`; } - const limitSql = limit ? sql` limit ${limit}` : undefined; + const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0) + ? sql` limit ${limit}` + : undefined; const operatorChunk = sql.raw(`${type} ${isAll ? 'all ' : ''}`); diff --git a/integration-tests/tests/mysql/mysql-common.ts b/integration-tests/tests/mysql/mysql-common.ts index d6d559ade..c96ae9319 100644 --- a/integration-tests/tests/mysql/mysql-common.ts +++ b/integration-tests/tests/mysql/mysql-common.ts @@ -3515,4 +3515,28 @@ export function tests(driver?: string) { await db.execute(sql`drop view ${newYorkers1}`); }); }); + + test('limit 0', async (ctx) => { + const { db } = ctx.mysql; + + await db.insert(usersTable).values({ name: 'John' }); + const users = await db + .select() + .from(usersTable) + .limit(0); + + expect(users).toEqual([]); + }); + + test('limit -1', async (ctx) => { + const { db } = ctx.mysql; + + await db.insert(usersTable).values({ name: 'John' }); + const users = await db + .select() + .from(usersTable) + .limit(-1); + + expect(users.length).toBeGreaterThan(0); + }); } diff --git a/integration-tests/tests/pg/pg-common.ts b/integration-tests/tests/pg/pg-common.ts index a94a1e348..fb69c5877 100644 --- a/integration-tests/tests/pg/pg-common.ts +++ b/integration-tests/tests/pg/pg-common.ts @@ -4457,5 +4457,29 @@ export function tests() { await db.execute(sql`drop materialized view ${newYorkers1}`); }); + + test('limit 0', async (ctx) => { + const { db } = ctx.pg; + + await db.insert(usersTable).values({ name: 'John' }); + const users = await db + .select() + .from(usersTable) + .limit(0); + + expect(users).toEqual([]); + }); + + test('limit -1', async (ctx) => { + const { db } = ctx.pg; + + await db.insert(usersTable).values({ name: 'John' }); + const users = await db + .select() + .from(usersTable) + .limit(-1); + + expect(users.length).toBeGreaterThan(0); + }); }); } diff --git a/integration-tests/tests/sqlite/sqlite-common.ts b/integration-tests/tests/sqlite/sqlite-common.ts index ae9b3d685..49c609941 100644 --- a/integration-tests/tests/sqlite/sqlite-common.ts +++ b/integration-tests/tests/sqlite/sqlite-common.ts @@ -2710,4 +2710,28 @@ export function tests() { expect(columnField?.isUnique).toBeTruthy(); expect(columnField?.uniqueName).toBe(uniqueKeyName(cities1Table, [columnField!.name])); }); + + test('limit 0', async (ctx) => { + const { db } = ctx.sqlite; + + await db.insert(usersTable).values({ name: 'John' }); + const users = await db + .select() + .from(usersTable) + .limit(0); + + expect(users).toEqual([]); + }); + + test('limit -1', async (ctx) => { + const { db } = ctx.sqlite; + + await db.insert(usersTable).values({ name: 'John' }); + const users = await db + .select() + .from(usersTable) + .limit(-1); + + expect(users.length).toBeGreaterThan(0); + }); }