From 73d2065227e651cf90a2cfade721516815743ab4 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 14 May 2026 14:06:46 +0900 Subject: [PATCH] fix: legacy peewee tables fk --- ...977_add_missing_primary_keys_to_legacy_.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 backend/open_webui/migrations/versions/461111b60977_add_missing_primary_keys_to_legacy_.py diff --git a/backend/open_webui/migrations/versions/461111b60977_add_missing_primary_keys_to_legacy_.py b/backend/open_webui/migrations/versions/461111b60977_add_missing_primary_keys_to_legacy_.py new file mode 100644 index 0000000000..c061e1febc --- /dev/null +++ b/backend/open_webui/migrations/versions/461111b60977_add_missing_primary_keys_to_legacy_.py @@ -0,0 +1,61 @@ +"""add missing primary keys to legacy peewee tables + +Revision ID: 461111b60977 +Revises: 3c9b0ca343fd +Create Date: 2026-05-14 04:38:14.000000 + +""" +from typing import Sequence, Union + +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision: str = '461111b60977' +down_revision: Union[str, None] = '3c9b0ca343fd' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + +# Tables bootstrapped by the old Peewee migration layer that may have +# UNIQUE(id) but no PRIMARY KEY constraint. Fresh Alembic installs +# already have correct PKs from 7e5b5dc7342b_init.py. +LEGACY_TABLES = [ + 'auth', 'chat', 'chatidtag', 'document', 'file', + 'function', 'memory', 'model', 'prompt', 'tag', 'tool', 'user', +] + + +def upgrade() -> None: + conn = op.get_bind() + inspector = sa.inspect(conn) + existing_tables = set(inspector.get_table_names()) + + for table_name in LEGACY_TABLES: + if table_name not in existing_tables: + continue + + pk = inspector.get_pk_constraint(table_name) + pk_cols = pk.get('constrained_columns', []) + + # Already has a proper PK on 'id' — nothing to do + if pk_cols == ['id']: + continue + + # Check that an 'id' column actually exists + columns = {c['name'] for c in inspector.get_columns(table_name)} + if 'id' not in columns: + continue + + print(f"Promoting UNIQUE(id) → PRIMARY KEY for '{table_name}'") + + with op.batch_alter_table(table_name) as batch_op: + # Drop existing PK if any (e.g. on wrong column) + if pk_cols and pk.get('name'): + batch_op.drop_constraint(pk['name'], type_='primary') + + batch_op.create_primary_key(f'pk_{table_name}_id', ['id']) + + +def downgrade() -> None: + # Downgrade is a no-op — we don't want to remove PKs + pass