mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-07 03:18:23 -05:00
[PR #22844] fix: add missing primary key constraints to legacy Peewee tables #49933
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?
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/22844
Author: @ryan-pip
Created: 3/19/2026
Status: 🔄 Open
Base:
dev← Head:fix/add-missing-primary-keys📝 Commits (2)
7f7cf8ffix: add missing primary key constraints to legacy Peewee tablesfcc99d9fix: catch edge case for no pks and check on downgrade📊 Changes
1 file changed (+58 additions, -0 deletions)
View changed files
➕
backend/open_webui/migrations/versions/c3d4e5f6a7b8_add_missing_primary_keys.py(+58 -0)📄 Description
Tables created by the Peewee migration system were created with only UNIQUE constraints on their
idcolumns, not PRIMARY KEY constraints. Alembic's init migration skips these tables since they already exist, so the PKs were never added. This migration retroactively promotes the unique constraint to a primary key for: chat, chatidtag, file, function, memory, model, tool.The upgrade() is idempotent — it inspects the existing PK constraint before acting. The redundant unique constraint on
idis dropped first.Pull Request Checklist
Note to first-time contributors: Please open a discussion post in Discussions to discuss your idea/fix with the community before creating a pull request, and describe your changes before submitting a pull request.
This is to ensure large feature PRs are discussed with the community first, before starting work on it. If the community does not want this feature or it is not relevant for Open WebUI as a project, it can be identified in the discussion before working on the feature and submitting the PR.
Before submitting, make sure you've checked the following:
devbranch. PRs targetingmainwill be immediately closed.devto ensure no unrelated commits (e.g. frommain) are included. Push updates to the existing PR branch instead of closing and reopening.Changelog Entry
Description
Tables originally created by the legacy Peewee migration system (
chat,chatidtag,file,function,memory,model,tool) were missingPRIMARY KEYconstraints ontheir
idcolumns. Peewee only adds aUNIQUEconstraint; when OpenWebUI migrated toAlembic, the init migration skipped these pre-existing tables, leaving the PKs absent. A
new Alembic migration retroactively fixes this.
Related discussion: https://github.com/open-webui/open-webui/discussions/22716
Fixed
PRIMARY KEYconstraints on legacy Peewee tables. Tables created by thelegacy Peewee migration system (
chat,chatidtag,file,function,memory,model,tool) were missingPRIMARY KEYconstraints on theiridcolumns. NewAlembic migration
c3d4e5f6a7b8retroactively promotes the unique constraint to aprimary key and drops the now-redundant unique constraint. The migration is idempotent —
it inspects the existing PK before acting, so it is safe on databases already migrated
through Alembic.
Additional Information
UNIQUEconstraint onidbut noPRIMARY KEY), then Alembic runs on top. Thismigration applies during that second phase on both new and existing installations.
downgrade()path drops the PK cleanly.Screenshots or Videos
Scenario A — Fresh install (
ghcr.io/open-webui/open-webui:dev, clean volume, migration mounted from start):INFO [alembic.runtime.migration] Context impl SQLiteImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.runtime.migration] Running upgrade -> 7e5b5dc7342b, init
...
INFO [alembic.runtime.migration] Running upgrade a1b2c3d4e5f6 -> b2c3d4e5f6a7, add scim column to user table
INFO [alembic.runtime.migration] Running upgrade b2c3d4e5f6a7 -> c3d4e5f6a7b8, add missing primary keys to legacy peewee tables
Schema after migration (
chattable):