[GH-ISSUE #1154] cli generate for rateLimit with database storage uses wrong pg column type for lastRequest resulting in error #8619

Closed
opened 2026-04-13 03:45:09 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @firxworx on GitHub (Jan 8, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1154

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Summary

It appears the table schema generated by the CLI for rate-limit with pg + drizzle uses postgres INTEGER for storing the lastRequest value. This type is too small for storing a JS timestamp (timestamp in milliseconds).

Using the generated schema produces an error.

Discovered on better-auth with drizzle + postgres using the Neon (drizzle-orm/neon-http) driver.

Context

  • postgres INTEGER default is a 4-byte signed int with max value +2147483647
  • an example of a current JS timestamp e.g. Date.now() e.g. 1736301491655 blows past this limit

Reproduction Steps

In server-side better-auth configuration enable with database storage:

rateLimit: {
  enabled: true,
  storage: 'database'
}

Generate an auth schema: pnpm dlx @better-auth/cli@latest generate

Inspect the output file. Note the resulting drizzle pgTable(...) schema definition is defined as follows:

export const rateLimit = pgTable("rate_limit", {
  id: text("id").primaryKey(),
  key: text('key'),
  count: integer('count'),
  lastRequest: integer('last_request')
});

Migrate the schema and then try to use the app with rate limiting and database storage.
Inspect the server console/log output for error information.

Fix / Workaround

Revise lastRequest to use postgres BIGINT which can handle a JS timestamp:

lastRequest: bigint('last_request', { mode: 'number' })

Above I use { mode: 'number' }) for convenience so the value is treated as a number on the JS/TS side; JS number can handle a JS millisecond timestamp.

Docs: https://orm.drizzle.team/docs/column-types/pg#bigint

Perhaps its worth doing a once-over of other better-auth schemas (and potentially queries too!) to make sure that there are no other cases of this class of pg column type vs. JS type incompatibility?

Thanks!

I'm trying out better-auth for the first time and so far its proving to be a much developer experience than other auth solutions I've worked with in this ecosystem!

Thank-you to the maintainers for better-auth!!

Current vs. Expected behavior

I expect the CLI to generate a postgres table schema that can support better-auth rateLimit with database storage feature when using postgres.

What version of Better Auth are you using?

1.1.0

Provide environment information

- OS: Ubuntu on WSL2
- Client: N/A

Which area(s) are affected? (Select all that apply)

Backend, Package

Auth config (if applicable)

// see reproduction steps for relevant auth config

Additional context

No response

Originally created by @firxworx on GitHub (Jan 8, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1154 ### Is this suited for github? - [X] Yes, this is suited for github ### To Reproduce ## Summary It appears the table schema generated by the CLI for rate-limit with pg + drizzle uses postgres `INTEGER` for storing the `lastRequest` value. This type is too small for storing a JS timestamp (timestamp in milliseconds). Using the generated schema produces an error. Discovered on better-auth with drizzle + postgres using the Neon (`drizzle-orm/neon-http`) driver. ## Context - postgres `INTEGER` default is a 4-byte signed int with max value +2147483647 - an example of a current JS timestamp e.g. `Date.now()` e.g. `1736301491655` blows past this limit ## Reproduction Steps In server-side better-auth configuration enable with `database` storage: ```ts rateLimit: { enabled: true, storage: 'database' } ``` Generate an auth schema: `pnpm dlx @better-auth/cli@latest generate` Inspect the output file. Note the resulting drizzle `pgTable(...)` schema definition is defined as follows: ```ts export const rateLimit = pgTable("rate_limit", { id: text("id").primaryKey(), key: text('key'), count: integer('count'), lastRequest: integer('last_request') }); ``` Migrate the schema and then try to use the app with rate limiting and database storage. Inspect the server console/log output for error information. ## Fix / Workaround Revise `lastRequest` to use postgres `BIGINT` which can handle a JS timestamp: ```ts lastRequest: bigint('last_request', { mode: 'number' }) ``` Above I use `{ mode: 'number' })` for convenience so the value is treated as a `number` on the JS/TS side; JS `number` can handle a JS millisecond timestamp. Docs: https://orm.drizzle.team/docs/column-types/pg#bigint Perhaps its worth doing a once-over of other better-auth schemas (and potentially queries too!) to make sure that there are no other cases of this class of pg column type vs. JS type incompatibility? ## Thanks! I'm trying out better-auth for the first time and so far its proving to be a much developer experience than other auth solutions I've worked with in this ecosystem! Thank-you to the maintainers for better-auth!! ### Current vs. Expected behavior I expect the CLI to generate a postgres table schema that can support better-auth rateLimit with database storage feature when using postgres. ### What version of Better Auth are you using? 1.1.0 ### Provide environment information ```bash - OS: Ubuntu on WSL2 - Client: N/A ``` ### Which area(s) are affected? (Select all that apply) Backend, Package ### Auth config (if applicable) ```typescript // see reproduction steps for relevant auth config ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-13 03:45:10 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8619