[GH-ISSUE #132] Suggestion: improve consistency across IDs #25465

Closed
opened 2026-04-17 15:41:56 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @rokitgg on GitHub (Oct 9, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/132

Description

Currently, there are inconsistencies in the string length of user and table IDs across the API. This can led to unexpected situations when, for example, a certain column might only allow for a maximum amount of characters to be inserted.

Imagine this scenario using drizzle-orm:

const users = pgTable("user", (t) => ({
   id: t.char({ length: 21 }).primaryKey();
  })
// some insert operations might fail while others don't!

Current Situation

I've observed varying approaches to ID generation across different parts of the codebase. For example:

  1. In user creation:

    const createdUser = await ctx.context.internalAdapter.createUser({
      id: generateRandomString(32, alphabet("a-z", "0-9", "A-Z")),
      // ...other fields
    });
    
  2. In a general ID generation function (utils/id.ts), where the default is set to 21 characters:

    export const generateId = (size?: number) => {
      return generateRandomString(size || 21, alphabet("a-z", "0-9", "A-Z"));
    };
    

Proposal

  1. Implement a common ID generation function that is used consistently across the entire codebase (already exists).
  2. Decide on a fixed length for IDs (e.g., 32 characters) to ensure uniformity.

Benefits

  • Improved code consistency and readability
  • Simplified database operations and queries
  • Reduced potential for conflicts or errors due to ID inconsistencies
  • Easier maintenance and debugging

Questions for Discussion

  • Is there a specific reason for the current variation in ID lengths?
Originally created by @rokitgg on GitHub (Oct 9, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/132 ### Description Currently, there are inconsistencies in the string length of user and table IDs across the API. This can led to unexpected situations when, for example, a certain column might only allow for a maximum amount of characters to be inserted. Imagine this scenario using drizzle-orm: ```typescript const users = pgTable("user", (t) => ({ id: t.char({ length: 21 }).primaryKey(); }) // some insert operations might fail while others don't! ``` ### Current Situation I've observed varying approaches to ID generation across different parts of the codebase. For example: 1. In user creation: ```typescript const createdUser = await ctx.context.internalAdapter.createUser({ id: generateRandomString(32, alphabet("a-z", "0-9", "A-Z")), // ...other fields }); ``` 2. In a general ID generation function (utils/id.ts), where the default is set to 21 characters: ```typescript export const generateId = (size?: number) => { return generateRandomString(size || 21, alphabet("a-z", "0-9", "A-Z")); }; ``` ### Proposal 1. Implement a common ID generation function that is used consistently across the entire codebase (already exists). 2. Decide on a fixed length for IDs (e.g., 32 characters) to ensure uniformity. ### Benefits - Improved code consistency and readability - Simplified database operations and queries - Reduced potential for conflicts or errors due to ID inconsistencies - Easier maintenance and debugging ### Questions for Discussion - Is there a specific reason for the current variation in ID lengths?
GiteaMirror added the locked label 2026-04-17 15:41:56 -05:00
Author
Owner

@Bekacru commented on GitHub (Oct 9, 2024):

Great suggestions! The variation was caused because we're refactoring to use generateId for all ID-related generations. I've just opened a PR that should address the issue.

<!-- gh-comment-id:2403298523 --> @Bekacru commented on GitHub (Oct 9, 2024): Great suggestions! The variation was caused because we're refactoring to use generateId for all ID-related generations. I've just opened a PR that should address the issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#25465