[PR #8416] feat(core): Support both mixed and numeric ids #7959

Open
opened 2026-03-13 13:54:38 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8416
Author: @OscarCornish
Created: 3/5/2026
Status: 🔄 Open

Base: canaryHead: feat/support-mixed-and-numeric-ids


📝 Commits (1)

  • afe3c79 Support both mixed and numeric ids

📊 Changes

12 files changed (+405 additions, -78 deletions)

View changed files

📝 e2e/adapter/test/adapter-factory/number-id.ts (+113 -1)
📝 e2e/adapter/test/memory-adapter/adapter.memory.test.ts (+2 -0)
📝 packages/better-auth/src/adapters/index.ts (+4 -0)
📝 packages/better-auth/src/db/get-migration.ts (+89 -34)
📝 packages/cli/src/generators/drizzle.ts (+28 -11)
📝 packages/cli/src/generators/prisma.ts (+40 -10)
📝 packages/core/src/db/adapter/factory.ts (+41 -13)
📝 packages/core/src/db/adapter/get-id-field.ts (+5 -2)
packages/core/src/db/adapter/is-number-id-model.ts (+23 -0)
📝 packages/core/src/types/init-options.ts (+23 -0)
📝 packages/memory-adapter/src/memory-adapter.ts (+36 -7)
📝 packages/telemetry/src/detectors/detect-auth-config.ts (+1 -0)

📄 Description

Adds advanced.database.useNumberId option to control which models use numeric (integer) IDs on a per-model basis.

Previously, generateId: "serial" forced all tables to use integer IDs. This was a problem for databases where only some tables use auto-increment integers (e.g. user.id is an integer) while others use
UUIDs. The string-to-number type coercion was globally tied to generateId === "serial", so there was no way to have mixed ID types.

Now you can do:

advanced: {
database: {
generateId: ({ model }) => model === "user" ? false : crypto.randomUUID(),
useNumberId: ["user"],
},
}

This makes only the user table use integer IDs while all other tables use UUIDs — and FK columns like session.userId are correctly coerced to integers when they reference a numeric-ID model.

Accepts true, false, string[], or (model: string) => boolean. When not set, falls back to legacy behavior (generateId: "serial" applies to all models).


Summary by cubic

Adds per-model control for numeric IDs with advanced.database.useNumberId, enabling mixed integer and string/UUID IDs across tables. Updates migrations, adapters, and schema generators to type IDs and FKs per model and coerce FK values correctly.

  • New Features

    • Added advanced.database.useNumberId (true/false, string[], or (model) => boolean). When unset, legacy generateId: "serial" applies to all models.
    • Migrations choose id/foreignKey column types per model for Postgres, MySQL, MSSQL, and SQLite.
    • Adapters coerce FK values to numbers only when referencing numeric-ID models; output types match the model’s ID type.
    • Drizzle and Prisma generators emit correct id/FK types and only import integer helpers when needed.
    • Telemetry now includes useNumberId.
  • Migration

    • Optional: set advanced.database.useNumberId to pick which models use integer IDs (e.g., ["user"]).

Written for commit afe3c7944b. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/8416 **Author:** [@OscarCornish](https://github.com/OscarCornish) **Created:** 3/5/2026 **Status:** 🔄 Open **Base:** `canary` ← **Head:** `feat/support-mixed-and-numeric-ids` --- ### 📝 Commits (1) - [`afe3c79`](https://github.com/better-auth/better-auth/commit/afe3c7944b554360635bb6c98ed6d1b5a10ebf9f) Support both mixed and numeric ids ### 📊 Changes **12 files changed** (+405 additions, -78 deletions) <details> <summary>View changed files</summary> 📝 `e2e/adapter/test/adapter-factory/number-id.ts` (+113 -1) 📝 `e2e/adapter/test/memory-adapter/adapter.memory.test.ts` (+2 -0) 📝 `packages/better-auth/src/adapters/index.ts` (+4 -0) 📝 `packages/better-auth/src/db/get-migration.ts` (+89 -34) 📝 `packages/cli/src/generators/drizzle.ts` (+28 -11) 📝 `packages/cli/src/generators/prisma.ts` (+40 -10) 📝 `packages/core/src/db/adapter/factory.ts` (+41 -13) 📝 `packages/core/src/db/adapter/get-id-field.ts` (+5 -2) ➕ `packages/core/src/db/adapter/is-number-id-model.ts` (+23 -0) 📝 `packages/core/src/types/init-options.ts` (+23 -0) 📝 `packages/memory-adapter/src/memory-adapter.ts` (+36 -7) 📝 `packages/telemetry/src/detectors/detect-auth-config.ts` (+1 -0) </details> ### 📄 Description Adds advanced.database.useNumberId option to control which models use numeric (integer) IDs on a per-model basis. Previously, generateId: "serial" forced all tables to use integer IDs. This was a problem for databases where only some tables use auto-increment integers (e.g. user.id is an integer) while others use UUIDs. The string-to-number type coercion was globally tied to generateId === "serial", so there was no way to have mixed ID types. Now you can do: advanced: { database: { generateId: ({ model }) => model === "user" ? false : crypto.randomUUID(), useNumberId: ["user"], }, } This makes only the user table use integer IDs while all other tables use UUIDs — and FK columns like session.userId are correctly coerced to integers when they reference a numeric-ID model. Accepts true, false, string[], or (model: string) => boolean. When not set, falls back to legacy behavior (generateId: "serial" applies to all models). <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds per-model control for numeric IDs with advanced.database.useNumberId, enabling mixed integer and string/UUID IDs across tables. Updates migrations, adapters, and schema generators to type IDs and FKs per model and coerce FK values correctly. - **New Features** - Added advanced.database.useNumberId (true/false, string[], or (model) => boolean). When unset, legacy generateId: "serial" applies to all models. - Migrations choose id/foreignKey column types per model for Postgres, MySQL, MSSQL, and SQLite. - Adapters coerce FK values to numbers only when referencing numeric-ID models; output types match the model’s ID type. - Drizzle and Prisma generators emit correct id/FK types and only import integer helpers when needed. - Telemetry now includes useNumberId. - **Migration** - Optional: set advanced.database.useNumberId to pick which models use integer IDs (e.g., ["user"]). <sup>Written for commit afe3c7944b554360635bb6c98ed6d1b5a10ebf9f. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 13:54:38 -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#7959