[GH-ISSUE #3800] Column type mismatch for teams #27050

Closed
opened 2026-04-17 17:51:02 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @enjunear on GitHub (Aug 5, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3800

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Create a auth.ts config file:

export const auth = betterAuth({
    database: drizzleAdapter(db, {
        provider: "pg",
    }),
    // ...
    advanced: {
        database: {
            useNumberId: true,
            generateId: false,
        },
    },
    // ...
    plugins: [
        organization({
            allowUserToCreateOrganization: false,
            teams: {
                enabled: true,
                allowRemovingAllTeams: false,
            },
        }),
        // ...other plugins
    ],
    // ... other config
});

run the schema generator cli

npx @better-auth/cli generate --config src/auth.ts

Current vs. Expected behavior

The team and teamMember tables are generated along with the rest of the schema:

export const team = pgTable("team", {
  id: serial("id").primaryKey(),
  name: text("name").notNull(),
  organizationId: integer("organization_id")
    .notNull()
    .references(() => organization.id, { onDelete: "cascade" }),
  createdAt: timestamp("created_at").notNull(),
  updatedAt: timestamp("updated_at"),
});

export const teamMember = pgTable("team_member", {
  id: serial("id").primaryKey(),
  teamId: text("team_id").notNull(),
  userId: integer("user_id")
    .notNull()
    .references(() => user.id, { onDelete: "cascade" }),
  createdAt: timestamp("created_at"),
});

Not the type on team.id is serial which should match to integer on teamMember.teamId, except it is text.

What version of Better Auth are you using?

0.0.1-27

Provide environment information

- OS: ubuntu 24.04
- Broswer: Firefox 141.0

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

Package

Auth config (if applicable)

export const auth = betterAuth({
    database: drizzleAdapter(db, {
        provider: "pg",
    }),
    // ...
    advanced: {
        database: {
            useNumberId: true,
            generateId: false,
        },
    },
    // ...
    plugins: [
        organization({
            allowUserToCreateOrganization: false,
            teams: {
                enabled: true,
                allowRemovingAllTeams: false,
            },
        }),
        // ...other plugins
    ],
    // ... other config
});

Additional context

No response

Originally created by @enjunear on GitHub (Aug 5, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3800 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Create a `auth.ts` config file: ```ts export const auth = betterAuth({ database: drizzleAdapter(db, { provider: "pg", }), // ... advanced: { database: { useNumberId: true, generateId: false, }, }, // ... plugins: [ organization({ allowUserToCreateOrganization: false, teams: { enabled: true, allowRemovingAllTeams: false, }, }), // ...other plugins ], // ... other config }); ``` run the schema generator cli ```bash npx @better-auth/cli generate --config src/auth.ts ``` ### Current vs. Expected behavior The `team` and `teamMember` tables are generated along with the rest of the schema: ```ts export const team = pgTable("team", { id: serial("id").primaryKey(), name: text("name").notNull(), organizationId: integer("organization_id") .notNull() .references(() => organization.id, { onDelete: "cascade" }), createdAt: timestamp("created_at").notNull(), updatedAt: timestamp("updated_at"), }); export const teamMember = pgTable("team_member", { id: serial("id").primaryKey(), teamId: text("team_id").notNull(), userId: integer("user_id") .notNull() .references(() => user.id, { onDelete: "cascade" }), createdAt: timestamp("created_at"), }); ``` Not the type on `team.id` is `serial` which should match to `integer` on `teamMember.teamId`, except it is `text`. ### What version of Better Auth are you using? 0.0.1-27 ### Provide environment information ```bash - OS: ubuntu 24.04 - Broswer: Firefox 141.0 ``` ### Which area(s) are affected? (Select all that apply) Package ### Auth config (if applicable) ```typescript export const auth = betterAuth({ database: drizzleAdapter(db, { provider: "pg", }), // ... advanced: { database: { useNumberId: true, generateId: false, }, }, // ... plugins: [ organization({ allowUserToCreateOrganization: false, teams: { enabled: true, allowRemovingAllTeams: false, }, }), // ...other plugins ], // ... other config }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-17 17:51:02 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 5, 2025):

Hi @enjunear! I'm Dosu and I’m helping the better-auth team.

This is a known bug in Better Auth's schema generator for PostgreSQL: when using the organization plugin with teams enabled, the generated teamMember.teamId column is text, which doesn't match the integer/serial type of team.id. This mismatch breaks referential integrity and can cause issues with migrations and queries. The root cause is in the schema generator logic for foreign keys when useNumberId is enabled—it incorrectly assigns text instead of integer to foreign key fields referencing id columns.

This was fixed in PR #3311, merged on 2025-07-17. The fix ensures that both primary and foreign key fields are generated as integer, so team.id and teamMember.teamId will match. To resolve this, upgrade to a version of Better Auth that includes this PR. If you can't upgrade, the only workaround is to manually edit the generated schema so that teamMember.teamId is integer, not text.

After the fix, the generated schema will look like:

export const team = pgTable("team", {
  id: integer("id").primaryKey(),
  // ...
});

export const teamMember = pgTable("team_member", {
  id: integer("id").primaryKey(),
  teamId: integer("team_id").notNull().references(() => team.id, { onDelete: "cascade" }),
  // ...
});

There is no config-based workaround in affected versions—manual schema edits or upgrading are required.
See PR #3311 for details.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

<!-- gh-comment-id:3154688453 --> @dosubot[bot] commented on GitHub (Aug 5, 2025): <!-- Greeting --> Hi @enjunear! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a known bug in Better Auth's schema generator for PostgreSQL: when using the organization plugin with teams enabled, the generated teamMember.teamId column is text, which doesn't match the integer/serial type of team.id. This mismatch breaks referential integrity and can cause issues with migrations and queries. The root cause is in the schema generator logic for foreign keys when useNumberId is enabled—it incorrectly assigns text instead of integer to foreign key fields referencing id columns. This was fixed in [PR #3311](https://github.com/better-auth/better-auth/pull/3311), merged on 2025-07-17. The fix ensures that both primary and foreign key fields are generated as integer, so team.id and teamMember.teamId will match. To resolve this, upgrade to a version of Better Auth that includes this PR. If you can't upgrade, the only workaround is to manually edit the generated schema so that teamMember.teamId is integer, not text. After the fix, the generated schema will look like: ```ts export const team = pgTable("team", { id: integer("id").primaryKey(), // ... }); export const teamMember = pgTable("team_member", { id: integer("id").primaryKey(), teamId: integer("team_id").notNull().references(() => team.id, { onDelete: "cascade" }), // ... }); ``` There is no config-based workaround in affected versions—manual schema edits or upgrading are required. [See PR #3311 for details](https://github.com/better-auth/better-auth/pull/3311). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/b5ea293e-c8db-4cc9-9a15-741efcf2680d?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/b5ea293e-c8db-4cc9-9a15-741efcf2680d?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/b5ea293e-c8db-4cc9-9a15-741efcf2680d?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/b5ea293e-c8db-4cc9-9a15-741efcf2680d?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/b5ea293e-c8db-4cc9-9a15-741efcf2680d?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/b5ea293e-c8db-4cc9-9a15-741efcf2680d?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/b5ea293e-c8db-4cc9-9a15-741efcf2680d?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/3800)
Author
Owner

@Kinfe123 commented on GitHub (Aug 7, 2025):

Should be fixed by now on latest beta.

<!-- gh-comment-id:3163008594 --> @Kinfe123 commented on GitHub (Aug 7, 2025): Should be fixed by now on latest beta.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#27050