fix:[FL-202][用户管理 - 创建用户 API 返回 500 错误]

修复了用户创建时因数据库表名不匹配导致的 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 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
chengkai3
2026-06-18 13:57:13 +08:00
parent 04d67c3d3d
commit d195bef4d3
+16 -7
View File
@@ -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: