From 06e916b5791ccdbc7ebded59bf861065ca9543fe Mon Sep 17 00:00:00 2001 From: sylarchen1389 Date: Sun, 7 Dec 2025 13:24:52 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E6=95=B0=E6=8D=AE=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E8=84=9A=E6=9C=AC=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../b2c3d4e5f6a7_fix_user_table_fields.py | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 backend/open_webui/migrations/versions/b2c3d4e5f6a7_fix_user_table_fields.py diff --git a/backend/open_webui/migrations/versions/b2c3d4e5f6a7_fix_user_table_fields.py b/backend/open_webui/migrations/versions/b2c3d4e5f6a7_fix_user_table_fields.py new file mode 100644 index 0000000000..129bd965ae --- /dev/null +++ b/backend/open_webui/migrations/versions/b2c3d4e5f6a7_fix_user_table_fields.py @@ -0,0 +1,51 @@ +"""Add missing username field to user table + +Revision ID: b2c3d4e5f6a7 +Revises: a1b2c3d4e5f6 +Create Date: 2025-12-07 15:00:00.000000 + +修复 user 表字段缺失问题: +- 添加 username 字段 (String(50), nullable) + +问题来源: +- username 字段在模型中存在但从未通过迁移添加 +""" + +from alembic import op +import sqlalchemy as sa + + +revision = "b2c3d4e5f6a7" +down_revision = "a1b2c3d4e5f6" +branch_labels = None +depends_on = None + + +def upgrade(): + """升级数据库:添加 username 字段""" + from sqlalchemy import inspect + + connection = op.get_bind() + inspector = inspect(connection) + + # 获取 user 表的现有列 + existing_columns = {col['name'] for col in inspector.get_columns('user')} + + # 添加 username 字段(仅当不存在时) + if "username" not in existing_columns: + op.add_column("user", sa.Column("username", sa.String(50), nullable=True)) + + +def downgrade(): + """降级数据库:删除 username 字段""" + from sqlalchemy import inspect + + connection = op.get_bind() + inspector = inspect(connection) + + # 获取 user 表的现有列 + existing_columns = {col['name'] for col in inspector.get_columns('user')} + + # 删除 username 字段(仅当存在时) + if "username" in existing_columns: + op.drop_column("user", "username")