mirror of
https://github.com/open-webui/open-webui.git
synced 2026-07-15 21:19:39 -05:00
[GH-ISSUE #24129] issue: Database migration fails with UniqueViolation and DuplicateTable errors when upgrading to latest version #107192
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @juanpabloequihua on GitHub (Apr 25, 2026).
Original GitHub issue: https://github.com/open-webui/open-webui/issues/24129
Check Existing Issues
Installation Method
Docker
Open WebUI Version
General Version v0.9.2
Ollama Version (if applicable)
No response
Operating System
Debian GNU/Linux
Browser (if applicable)
No response
Confirmation
README.md.Expected Behavior
Expected Behavior
Database migrations should run successfully regardless of:
Specifically:
before attempting to create them
access_grantrecords in migrationc1d2e3f4a5b6) should check for duplicate records before inserting andgracefully skip them if they already exist
without crashing on already-created database objects
without requiring manual database intervention
Actual Behavior
Actual Behavior
The migration chain fails with multiple errors on a large PostgreSQL database (100GB+)
that has existing shared chats and data accumulated over time.
The failure occurs in two distinct ways:
1. UniqueViolation (migration c1d2e3f4a5b6)
The migration script loops through all existing shared chats and attempts to insert
a
readpermission record into theaccess_granttable for each one. Because thedatabase already contains these records from a previous partial migration attempt,
the plain
INSERTstatement crashes with:sqlalchemy.exc.IntegrityError: (psycopg2.errors.UniqueViolation)
duplicate key value violates unique constraint "uq_access_grant_grant"
2. DuplicateTable errors (migrations d4e5f6a7b8c9 and 56359461a091)
When the migration chain crashes midway, some tables are created but the Alembic
version is never updated. On the next migration attempt, the scripts try to create
those tables again, resulting in:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DuplicateTable)
relation "automation" already exists
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DuplicateTable)
relation "calendar" already exists
3. Inconsistent database state
The combination of the above errors leaves the database in an inconsistent state
where:
tasks,summary,last_read_at,is_pinned)psycopg.errors.UndefinedColumn: column "tasks" of relation "chat" does not exist
psycopg.errors.UndefinedTable: relation "automation" does not exist
4. Recovery requires complex manual intervention
Recovering from this state required:
Note: The database size (100GB+) is likely a contributing factor as it increases
the number of shared chats and accumulated data, making duplicate record conflicts
more likely to occur during data migration steps.
Steps to Reproduce
Steps to Reproduce
Have a running Open WebUI instance with:
Pull the latest Docker image:
Stop the current container:
Start a new container with the latest image:
Check the container logs:
Observe the migration errors in the logs. The failure chain is:
c1d2e3f4a5b6crashes withUniqueViolationDuplicateTableerrorsUndefinedColumnerrorsUndefinedTableerrorsLogs & Screenshots
Logs
Initial migration failure (container logs)
DuplicateTable error after partial migration (subsequent restart attempt)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DuplicateTable)
relation "calendar" already exists
[SQL:
CREATE TABLE calendar (
id TEXT NOT NULL,
user_id TEXT NOT NULL,
name TEXT NOT NULL,
color TEXT,
is_default BOOLEAN NOT NULL,
...
)
]
Application errors after inconsistent migration state
Additional Information
Additional Information
Database Details
of existing shared chat records, making the
UniqueViolationin migrationc1d2e3f4a5b6more likely to occurDeployment Details
ghcr.io/open-webui/open-webui:mainAffected Migration Files
All of the following migration scripts lack existence checks:
a3dd5bedd151d4e5f6a7b8c9b7c8d9e0f1a2e1f2a3b4c5d6c1d2e3f4a5b656359461a091Complete Workaround & Solution
The following steps were required to fully recover from the broken migration state.
This is documented here in detail to help other users who encounter the same issue
and to provide the maintainers with a clear picture of the impact on end users.
Step 1: Enter the Docker container
Step 2: Export required environment variables
The migration scripts require these two environment variables to initialize
the application environment. Without them, Alembic will fail with
ValueError: Required environment variable not found:Step 3: Edit all affected migration files
Each migration file needs to be updated to add existence checks before creating
or inserting database objects. The files are located at:
The required code changes for each file are as follows:
Fix for table creation (migrations
d4e5f6a7b8c9and56359461a091):Fix for column addition (migrations
a3dd5bedd151,b7c8d9e0f1a2,e1f2a3b4c5d6):Fix for duplicate data inserts (migration
c1d2e3f4a5b6):Step 4: Roll back the Alembic stamp to the last known good migration
This is necessary to force Alembic to re-execute all the migrations that were
partially applied or skipped. The last known good migration before the failures
began was
b2c3d4e5f6a7:Step 5: Re-run the migrations
A successful run should show all 6 migrations completing without errors:
Step 6: Restart the container
Impact Assessment
This bug has a significant impact on production deployments:
SQLAlchemy and Alembic - not suitable for non-technical users
fully understanding the consequences
Recommendation
In addition to fixing the individual migration scripts, we recommend considering
the following improvements to make the migration system more robust:
database before running migrations and warns the user if inconsistencies are found
migration fails
the database is automatically restored to its previous consistent state
these issues before release
@bitsofinfo commented on GitHub (Apr 27, 2026):
same get this upgrading from 0.8.10 to 0.9.2
@AndreasUpb commented on GitHub (Apr 28, 2026):
Thanks for writing fail-safe migrations. @juanpabloequihua
Is it fine to ask you for a PR? Our setup only broke on c1d2e3f4a5b6 - Included the patch as described helped a lot to get it working back
/app/backend/open_webui/migrations/versions/c1d2e3f4a5b6_add_shared_chat_table.py
@juanpabloequihua commented on GitHub (Apr 28, 2026):
@AndreasUpb no problem, I have submitted the PR to resolve this: #24197
The PR adds idempotency (existence checks) to the 6 affected migration files mentioned in this issue. I have manually verified that these changes allow the migrations to complete successfully on a large PostgreSQL database after the failed migrations.
For reference, here is the log output of the successful migration after applying these changes to my large PostgreSQL instance:
@Classic298 commented on GitHub (May 17, 2026):
this is fixed in dev - confirmation wanted