From d195bef4d368fc562b692566767443dc3aa904c0 Mon Sep 17 00:00:00 2001 From: chengkai3 Date: Thu, 18 Jun 2026 13:57:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:[FL-202][=E7=94=A8=E6=88=B7=E7=AE=A1?= =?UTF-8?q?=E7=90=86=20-=20=E5=88=9B=E5=BB=BA=E7=94=A8=E6=88=B7=20API=20?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=20500=20=E9=94=99=E8=AF=AF]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复了用户创建时因数据库表名不匹配导致的 500 错误。 问题原因: 代码中使用了错误的数据库表名: - 使用 user_role 表,但实际表名是 roles - 使用 user_role_rela 表,但实际表名是 user_roles - 查询 user_role.id = 'user',但应该是 roles.code = 'user' 修改内容: 1. _role_ids_exist(): 修正表名从 user_role 到 roles,字段从 id 到 code 2. _replace_legacy_user_roles(): 修正表名从 user_role_rela 到 user_roles,并添加通过 role_code 查询 role_id 的逻辑 3. _assign_legacy_roles(): 修正默认角色查询从 user_role.id 到 roles.code Co-Authored-By: Claude Sonnet 4.6 Co-authored-by: multica-agent --- api/app/services/user_service.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/api/app/services/user_service.py b/api/app/services/user_service.py index c89519c..5a9364f 100644 --- a/api/app/services/user_service.py +++ b/api/app/services/user_service.py @@ -504,7 +504,7 @@ def _to_storage_user_status(raw_status: str) -> str: def _role_ids_exist(db: Session, role_ids: list[str]) -> bool: stmt = text( - "SELECT id FROM user_role WHERE id IN :role_ids" + "SELECT code FROM roles WHERE code IN :role_ids" ).bindparams(bindparam("role_ids", expanding=True)) try: existing = {str(row[0]) for row in db.execute(stmt, {"role_ids": role_ids}).all()} @@ -513,20 +513,29 @@ def _role_ids_exist(db: Session, role_ids: list[str]) -> bool: return set(role_ids).issubset(existing) -def _replace_legacy_user_roles(db: Session, user_id: str, role_ids: list[str]) -> bool: +def _replace_legacy_user_roles(db: Session, user_id: str, role_codes: list[str]) -> bool: try: - db.execute(text("DELETE FROM user_role_rela WHERE user_id = :user_id"), {"user_id": user_id}) + db.execute(text("DELETE FROM user_roles WHERE user_id = :user_id"), {"user_id": user_id}) + + # Get role IDs from role codes + role_id_stmt = text( + "SELECT id FROM roles WHERE code IN :role_codes" + ).bindparams(bindparam("role_codes", expanding=True)) + role_ids = [row[0] for row in db.execute(role_id_stmt, {"role_codes": role_codes}).all()] + + if not role_ids: + return False + insert_stmt = text( """ - INSERT INTO user_role_rela (rela_id, user_id, role_id) - VALUES (:rela_id, :user_id, :role_id) + INSERT INTO user_roles (user_id, role_id) + VALUES (:user_id, :role_id) """ ) for role_id in role_ids: db.execute( insert_stmt, { - "rela_id": uuid4().hex, "user_id": user_id, "role_id": role_id, }, @@ -542,7 +551,7 @@ def _assign_legacy_roles(db: Session, user_id: str, role_ids: list[str]) -> None # Keep create-user path backward compatible: if no explicit role given, try legacy "user". if not normalized: try: - exists = db.scalar(text("SELECT id FROM user_role WHERE id = 'user' LIMIT 1")) + exists = db.scalar(text("SELECT code FROM roles WHERE code = 'user' LIMIT 1")) if exists: normalized = ["user"] except SQLAlchemyError: