[changes] The pre-query at the service layer has been removed. The DB constraint ensures a unique single source of truth.
This commit is contained in:
@@ -100,20 +100,16 @@ def create_config(
|
|||||||
svc = DataConfigService(db)
|
svc = DataConfigService(db)
|
||||||
result = svc.create(payload)
|
result = svc.create(payload)
|
||||||
return success(data=result, msg="创建成功")
|
return success(data=result, msg="创建成功")
|
||||||
except ValueError as e:
|
except Exception as e:
|
||||||
err_str = str(e)
|
from sqlalchemy.exc import IntegrityError
|
||||||
if err_str.startswith("DUPLICATE_CONFIG_NAME:"):
|
if isinstance(e, IntegrityError) and "uq_workspace_config_name" in str(getattr(e, 'orig', '')):
|
||||||
config_name = err_str.split(":", 1)[1]
|
api_logger.warning(f"重复的配置名称 '{payload.config_name}' 在工作空间 {workspace_id}")
|
||||||
api_logger.warning(f"重复的配置名称 '{config_name}' 在工作空间 {workspace_id}")
|
|
||||||
lang = get_language_from_header(x_language_type)
|
lang = get_language_from_header(x_language_type)
|
||||||
if lang == "en":
|
if lang == "en":
|
||||||
msg = fail(BizCode.BAD_REQUEST, "Config name already exists", f"A config named \"{config_name}\" already exists in the current workspace. Please use a different name.")
|
msg = fail(BizCode.BAD_REQUEST, "Config name already exists", f"A config named \"{payload.config_name}\" already exists in the current workspace. Please use a different name.")
|
||||||
else:
|
else:
|
||||||
msg = fail(BizCode.BAD_REQUEST, "配置名称已存在", f"当前工作空间下已存在名为「{config_name}」的记忆配置,请使用其他名称")
|
msg = fail(BizCode.BAD_REQUEST, "配置名称已存在", f"当前工作空间下已存在名为「{payload.config_name}」的记忆配置,请使用其他名称")
|
||||||
return JSONResponse(status_code=400, content=msg)
|
return JSONResponse(status_code=400, content=msg)
|
||||||
api_logger.error(f"Create config failed: {err_str}")
|
|
||||||
return fail(BizCode.INTERNAL_ERROR, "创建配置失败", err_str)
|
|
||||||
except Exception as e:
|
|
||||||
api_logger.error(f"Create config failed: {str(e)}")
|
api_logger.error(f"Create config failed: {str(e)}")
|
||||||
return fail(BizCode.INTERNAL_ERROR, "创建配置失败", str(e))
|
return fail(BizCode.INTERNAL_ERROR, "创建配置失败", str(e))
|
||||||
|
|
||||||
|
|||||||
@@ -115,17 +115,6 @@ class DataConfigService: # 数据配置服务类(PostgreSQL)
|
|||||||
|
|
||||||
# --- Create ---
|
# --- Create ---
|
||||||
def create(self, params: ConfigParamsCreate) -> Dict[str, Any]: # 创建配置参数(仅名称与描述)
|
def create(self, params: ConfigParamsCreate) -> Dict[str, Any]: # 创建配置参数(仅名称与描述)
|
||||||
# 检查同一工作空间下是否已存在同名配置
|
|
||||||
if params.workspace_id and params.config_name:
|
|
||||||
from app.models.memory_config_model import MemoryConfig
|
|
||||||
existing = (
|
|
||||||
self.db.query(MemoryConfig)
|
|
||||||
.filter_by(workspace_id=params.workspace_id, config_name=params.config_name)
|
|
||||||
.first()
|
|
||||||
)
|
|
||||||
if existing:
|
|
||||||
raise ValueError(f"DUPLICATE_CONFIG_NAME:{params.config_name}")
|
|
||||||
|
|
||||||
# 如果workspace_id存在且模型字段未全部指定,则自动获取
|
# 如果workspace_id存在且模型字段未全部指定,则自动获取
|
||||||
if params.workspace_id and not all([params.llm_id, params.embedding_id, params.rerank_id]):
|
if params.workspace_id and not all([params.llm_id, params.embedding_id, params.rerank_id]):
|
||||||
configs = self._get_workspace_configs(params.workspace_id)
|
configs = self._get_workspace_configs(params.workspace_id)
|
||||||
|
|||||||
Reference in New Issue
Block a user