refactor: Enhance unique constraint validation across data operations
- Integrated `TableManagementService` to validate unique constraints before insert, update, and upsert actions in various controllers, including `dataflowExecutionController`, `dynamicFormController`, and `tableManagementController`. - Improved error handling in `errorHandler` to provide detailed messages indicating which field has a unique constraint violation. - Updated the `formatPgError` utility to extract and display specific column labels for unique constraint violations, enhancing user feedback. - Adjusted the table schema retrieval to include company-specific nullable and unique constraints, ensuring accurate representation of database rules. These changes improve data integrity by preventing duplicate entries and enhance user experience through clearer error messages related to unique constraints.
This commit is contained in:
@@ -37,8 +37,38 @@ export async function formatPgError(
|
||||
const detail = colName ? ` [${colName}]` : "";
|
||||
return `필수 입력값이 누락되었습니다.${detail}`;
|
||||
}
|
||||
case "23505":
|
||||
return "중복된 데이터가 존재합니다.";
|
||||
case "23505": {
|
||||
// unique_violation
|
||||
const constraint = error.constraint || "";
|
||||
const tblName = error.table || "";
|
||||
// constraint 이름에서 컬럼명 추출 시도 (예: item_mst_item_code_key → item_code)
|
||||
let colName = "";
|
||||
if (constraint && tblName) {
|
||||
const prefix = `${tblName}_`;
|
||||
const suffix = "_key";
|
||||
if (constraint.startsWith(prefix) && constraint.endsWith(suffix)) {
|
||||
colName = constraint.slice(prefix.length, -suffix.length);
|
||||
}
|
||||
}
|
||||
if (colName && tblName && companyCode) {
|
||||
try {
|
||||
const rows = await query(
|
||||
`SELECT column_label FROM table_type_columns
|
||||
WHERE table_name = $1 AND column_name = $2 AND company_code = $3
|
||||
LIMIT 1`,
|
||||
[tblName, colName, companyCode]
|
||||
);
|
||||
const label = rows[0]?.column_label;
|
||||
if (label) {
|
||||
return `중복된 데이터가 존재합니다: ${label}`;
|
||||
}
|
||||
} catch {
|
||||
// 폴백
|
||||
}
|
||||
}
|
||||
const detail = colName ? ` [${colName}]` : "";
|
||||
return `중복된 데이터가 존재합니다.${detail}`;
|
||||
}
|
||||
case "23503":
|
||||
return "참조 무결성 제약 조건 위반입니다.";
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user