[GH-ISSUE #6832] [organization] Migration fails: team table created before organization table #10646

Closed
opened 2026-04-13 06:54:15 -05:00 by GiteaMirror · 7 comments
Owner

Originally created by @nynj on GitHub (Dec 17, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/6832

Description:

When using the organization plugin with teams enabled, the migrate command fails because it attempts to create the team table (which has a foreign key to organization) before creating the organization table.

Versions:

  • better-auth: 1.4.7
  • @better-auth/cli: 1.4.7
  • Database: PostgreSQL 16

Configuration:
import { betterAuth } from "better-auth";
import { organization } from "better-auth/plugins";

export const auth = betterAuth({
database: new Pool({ connectionString: process.env.DATABASE_URL }),
plugins: [
organization({
teams: { enabled: true }
})
],
});

Steps to reproduce:

  1. Configure better-auth with the organization plugin (teams enabled)
  2. Run npx @better-auth/cli migrate -y against an empty PostgreSQL database

Expected behavior:

Tables created in dependency order:

  1. organization table (no dependencies)
  2. team table (depends on organization)

Actual behavior:

The CLI outputs migration plan showing team before organization:
🔑 The migration will affect the following:
-> name, organizationId, createdAt, updatedAt fields on team table.
-> name, slug, logo, createdAt, metadata fields on organization table.

Then fails with:
error: relation "organization" does not exist

Workaround:

Use better-auth generate to get schema, create organization first, then run migrate:

npx @better-auth/cli generate -y --output /tmp/schema.sql
grep 'create table "organization"' /tmp/schema.sql | psql -d mydb
npx @better-auth/cli migrate -y # now succeeds

Related:

  • #5473 fixed similar ordering for dynamicAccessControl, but base organization plugin still has this issue
Originally created by @nynj on GitHub (Dec 17, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/6832 Description: When using the organization plugin with teams enabled, the migrate command fails because it attempts to create the team table (which has a foreign key to organization) before creating the organization table. Versions: - better-auth: 1.4.7 - @better-auth/cli: 1.4.7 - Database: PostgreSQL 16 Configuration: import { betterAuth } from "better-auth"; import { organization } from "better-auth/plugins"; export const auth = betterAuth({ database: new Pool({ connectionString: process.env.DATABASE_URL }), plugins: [ organization({ teams: { enabled: true } }) ], }); Steps to reproduce: 1. Configure better-auth with the organization plugin (teams enabled) 2. Run npx @better-auth/cli migrate -y against an empty PostgreSQL database Expected behavior: Tables created in dependency order: 1. organization table (no dependencies) 2. team table (depends on organization) Actual behavior: The CLI outputs migration plan showing team before organization: 🔑 The migration will affect the following: -> name, organizationId, createdAt, updatedAt fields on team table. -> name, slug, logo, createdAt, metadata fields on organization table. Then fails with: error: relation "organization" does not exist Workaround: Use better-auth generate to get schema, create organization first, then run migrate: npx @better-auth/cli generate -y --output /tmp/schema.sql grep 'create table "organization"' /tmp/schema.sql | psql -d mydb npx @better-auth/cli migrate -y # now succeeds Related: - #5473 fixed similar ordering for dynamicAccessControl, but base organization plugin still has this issue
GiteaMirror added the databaselockedbugorganization labels 2026-04-13 06:54:15 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Dec 17, 2025):

Thanks for opening this issue! It seems that when the organization plugin is used with teams enabled, the migration order causes the team table—which depends on the organization table—to be created first, leading to a failure. As a workaround, you can generate the schema using the CLI (for example, npx @better-auth/cli generate -y --output /tmp/schema.sql), manually create the organization table first (e.g., grep for the organization table creation command from the generated file), and then run the migrate command. This approach is similar to what other users have experienced where dynamic table ordering was an issue.

Have you already tried the manual schema generation workaround, or do you need further assistance with automating the ordering? Tag @better-auth-agent if you would like an answer from the Agent.

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3666600904 --> @better-auth-agent[bot] commented on GitHub (Dec 17, 2025): Thanks for opening this issue! It seems that when the organization plugin is used with teams enabled, the migration order causes the team table—which depends on the organization table—to be created first, leading to a failure. As a workaround, you can generate the schema using the CLI (for example, npx @better-auth/cli generate -y --output /tmp/schema.sql), manually create the organization table first (e.g., grep for the organization table creation command from the generated file), and then run the migrate command. This approach is similar to what other users have experienced where dynamic table ordering was an issue. Have you already tried the manual schema generation workaround, or do you need further assistance with automating the ordering? Tag @better-auth-agent if you would like an answer from the Agent. _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@dosubot[bot] commented on GitHub (Dec 17, 2025):

This migration failure happens because better-auth determines table creation order using a numeric order property in the schema. Core tables have explicit order values, but plugin tables like organization and team default to Infinity and are created in the order they appear in the schema object, which can be unpredictable. In your case, the team table is created before the organization table, causing foreign key errors.

A similar migration order bug was fixed for the organization plugin when dynamicAccessControl is enabled (#5473, PR #5476), but this fix does not apply to the base organization+team scenario as of 1.4.7. There is currently no configuration or user-facing way to control migration order for plugin tables; the only workarounds are to manually create the organization table first (as you did), or modify the plugin code to assign explicit order values to ensure dependency order is respected.

For now, your manual workaround is the recommended approach until a fix is released for this scenario.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

<!-- gh-comment-id:3666620022 --> @dosubot[bot] commented on GitHub (Dec 17, 2025): <!-- Answer --> This migration failure happens because better-auth determines table creation order using a numeric `order` property in the schema. Core tables have explicit order values, but plugin tables like organization and team default to `Infinity` and are created in the order they appear in the schema object, which can be unpredictable. In your case, the team table is created before the organization table, causing foreign key errors. A similar migration order bug was fixed for the organization plugin when `dynamicAccessControl` is enabled ([#5473](https://github.com/better-auth/better-auth/issues/5473), [PR #5476](https://github.com/better-auth/better-auth/pull/5476)), but this fix does not apply to the base organization+team scenario as of 1.4.7. There is currently no configuration or user-facing way to control migration order for plugin tables; the only workarounds are to manually create the organization table first (as you did), or modify the plugin code to assign explicit `order` values to ensure dependency order is respected. For now, your manual workaround is the recommended approach until a fix is released for this scenario. <!-- 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/e0f51bf3-3f1c-4dd0-a839-279931cbb15e?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/e0f51bf3-3f1c-4dd0-a839-279931cbb15e?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/e0f51bf3-3f1c-4dd0-a839-279931cbb15e?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/e0f51bf3-3f1c-4dd0-a839-279931cbb15e?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/e0f51bf3-3f1c-4dd0-a839-279931cbb15e?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/e0f51bf3-3f1c-4dd0-a839-279931cbb15e?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/e0f51bf3-3f1c-4dd0-a839-279931cbb15e?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=This%20migration%20failure%20happens%20because%20better-auth%20determines%20table%20creation%20order%20using%20a%20numeric%20%60order%60%20property%20in%20the%20schema.%20Core%20tables%20have%20explicit%20order%20values%2C%20but%20plugin%20tables%20like%20organization%20and%20team%20default%20to%20%60Infinity%60%20and%20are%20created%20in%20the%20order%20they%20appear%20in%20the%20schema%20object%2C%20which%20can%20be%20unpredictable.%20In%20your%20case%2C%20the%20team%20table%20is%20created%20before%20the%20organization%20table%2C%20causing%20foreign%20key%20errors.%0A%0AA%20similar%20migration%20order%20bug%20was%20fixed%20for%20the%20organization%20plugin%20when%20%60dynamicAccessControl%60%20is%20enabled%20%28%5B%235473%5D%28https%3A//github.com/better-auth/better-auth/issues/5473%29%2C%20%5BPR%20%235476%5D%28https%3A//github.com/better-auth/better-auth/pull/5476%29%29%2C%20but%20this%20fix%20does%20not%20apply%20to%20the%20base%20organization%2Bteam%20scenario%20as%20of%201.4.7.%20There%20is%20currently%20no%20configuration%20or%20user-facing%20way%20to%20control%20migration%20order%20for%20plugin%20tables%3B%20the%20only%20workarounds%20are%20to%20manually%20create%20the%20organization%20table%20first%20%28as%20you%20did%29%2C%20or%20modify%20the%20plugin%20code%20to%20assign%20explicit%20%60order%60%20values%20to%20ensure%20dependency%20order%20is%20respected.%0A%0AFor%20now%2C%20your%20manual%20workaround%20is%20the%20recommended%20approach%20until%20a%20fix%20is%20released%20for%20this%20scenario.)&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/6832)
Author
Owner

@surafel58 commented on GitHub (Dec 19, 2025):

Is anyone working on this issue ? @better-auth-agent

<!-- gh-comment-id:3676306593 --> @surafel58 commented on GitHub (Dec 19, 2025): Is anyone working on this issue ? @better-auth-agent
Author
Owner

@better-auth-agent[bot] commented on GitHub (Dec 19, 2025):

There is currently an open issue regarding the migration failure when using the organization plugin with teams enabled. The problem arises because the migration command attempts to create the team table, which has a foreign key dependency on the organization table, before the organization table itself is created. This results in an error indicating that the "organization" relation does not exist when the migration is run.

As a temporary workaround, it is advised to manually generate the schema, create the organization table first, and then proceed with the migration as follows:

  1. Generate the schema:
    npx @better-auth/cli generate -y --output /tmp/schema.sql
    
  2. Create the organization table manually:
    grep 'create table "organization"' /tmp/schema.sql | psql -d mydb
    
  3. Run the migration again:
    npx @better-auth/cli migrate -y
    

Additionally, this issue has been recorded as related to a previous fix (issue #5473) which addressed a similar ordering issue but does not yet resolve the organization plugin's problems with the team table migration.

Is there anything specific you would like to know more about this issue, or perhaps you have another question?

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3676307187 --> @better-auth-agent[bot] commented on GitHub (Dec 19, 2025): There is currently an open issue regarding the migration failure when using the organization plugin with teams enabled. The problem arises because the migration command attempts to create the team table, which has a foreign key dependency on the organization table, before the organization table itself is created. This results in an error indicating that the "organization" relation does not exist when the migration is run. As a temporary workaround, it is advised to manually generate the schema, create the organization table first, and then proceed with the migration as follows: 1. Generate the schema: ```bash npx @better-auth/cli generate -y --output /tmp/schema.sql ``` 2. Create the organization table manually: ```bash grep 'create table "organization"' /tmp/schema.sql | psql -d mydb ``` 3. Run the migration again: ```bash npx @better-auth/cli migrate -y ``` Additionally, this issue has been recorded as related to a previous fix (issue #5473) which addressed a similar ordering issue but does not yet resolve the organization plugin's problems with the team table migration. Is there anything specific you would like to know more about this issue, or perhaps you have another question? <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@surafel58 commented on GitHub (Dec 19, 2025):

@nynj @Kinfe123 I wanna work on this issue.

<!-- gh-comment-id:3676467949 --> @surafel58 commented on GitHub (Dec 19, 2025): @nynj @Kinfe123 I wanna work on this issue.
Author
Owner

@dosubot[bot] commented on GitHub (Mar 20, 2026):

Hi, @nynj. I'm Dosu, and I'm helping the better-auth team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You reported a migration failure caused by the team table being created before the organization table when using the organization plugin with teams enabled.
  • This results in foreign key errors due to the undefined creation order of plugin tables.
  • The current workaround is manually creating the organization table first from the generated schema.
  • A related fix exists for a similar issue with dynamicAccessControl enabled, but not for this base scenario as of version 1.4.7.
  • Another user expressed interest in working on a fix, but the issue remains unresolved.

Next Steps:

  • Please let me know if this issue is still relevant with the latest version of better-auth by commenting here to keep the discussion open.
  • If I don’t hear back within 7 days, I will automatically close this issue.

Thanks for your understanding and contribution!

<!-- gh-comment-id:4099277936 --> @dosubot[bot] commented on GitHub (Mar 20, 2026): Hi, @nynj. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary:** - You reported a migration failure caused by the team table being created before the organization table when using the organization plugin with teams enabled. - This results in foreign key errors due to the undefined creation order of plugin tables. - The current workaround is manually creating the organization table first from the generated schema. - A related fix exists for a similar issue with dynamicAccessControl enabled, but not for this base scenario as of version 1.4.7. - Another user expressed interest in working on a fix, but the issue remains unresolved. **Next Steps:** - Please let me know if this issue is still relevant with the latest version of better-auth by commenting here to keep the discussion open. - If I don’t hear back within 7 days, I will automatically close this issue. Thanks for your understanding and contribution!
Author
Owner

@github-actions[bot] commented on GitHub (Apr 4, 2026):

This issue has been locked as it was closed more than 7 days ago. If you're experiencing a similar problem or you have additional context, please open a new issue and reference this one.

<!-- gh-comment-id:4185770971 --> @github-actions[bot] commented on GitHub (Apr 4, 2026): This issue has been locked as it was closed more than 7 days ago. If you're experiencing a similar problem or you have additional context, please open a new issue and reference this one.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#10646