조인 컬럼 문제 수정
This commit is contained in:
@@ -67,14 +67,24 @@ export class EntityJoinService {
|
||||
separator,
|
||||
screenConfig,
|
||||
});
|
||||
} else if (column.display_column) {
|
||||
// 기존 설정된 단일 표시 컬럼 사용
|
||||
} else if (column.display_column && column.display_column !== "none") {
|
||||
// 기존 설정된 단일 표시 컬럼 사용 (none이 아닌 경우만)
|
||||
displayColumns = [column.display_column];
|
||||
} else {
|
||||
// 화면에서 설정하도록 빈 배열로 초기화 (테이블 타입 관리에서 표시 컬럼 설정 제거)
|
||||
displayColumns = [];
|
||||
// 조인 탭에서 보여줄 기본 표시 컬럼 설정
|
||||
// dept_info 테이블의 경우 dept_name을 기본으로 사용
|
||||
let defaultDisplayColumn = column.reference_column;
|
||||
if (column.reference_table === "dept_info") {
|
||||
defaultDisplayColumn = "dept_name";
|
||||
} else if (column.reference_table === "company_info") {
|
||||
defaultDisplayColumn = "company_name";
|
||||
} else if (column.reference_table === "user_info") {
|
||||
defaultDisplayColumn = "user_name";
|
||||
}
|
||||
|
||||
displayColumns = [defaultDisplayColumn];
|
||||
console.log(
|
||||
`🎯 표시 컬럼을 화면에서 설정하도록 초기화: ${column.column_name} (테이블 타입 관리에서 표시 컬럼 설정 제거됨)`
|
||||
`🔧 조인 탭용 기본 표시 컬럼 설정: ${column.column_name} → ${defaultDisplayColumn} (${column.reference_table})`
|
||||
);
|
||||
}
|
||||
|
||||
@@ -119,8 +129,10 @@ export class EntityJoinService {
|
||||
offset?: number
|
||||
): { query: string; aliasMap: Map<string, string> } {
|
||||
try {
|
||||
// 기본 SELECT 컬럼들
|
||||
const baseColumns = selectColumns.map((col) => `main.${col}`).join(", ");
|
||||
// 기본 SELECT 컬럼들 (TEXT로 캐스팅하여 record 타입 오류 방지)
|
||||
const baseColumns = selectColumns
|
||||
.map((col) => `main.${col}::TEXT AS ${col}`)
|
||||
.join(", ");
|
||||
|
||||
// Entity 조인 컬럼들 (COALESCE로 NULL을 빈 문자열로 처리)
|
||||
// 별칭 매핑 생성 (JOIN 절과 동일한 로직)
|
||||
@@ -181,9 +193,9 @@ export class EntityJoinService {
|
||||
].includes(col);
|
||||
|
||||
if (isJoinTableColumn) {
|
||||
return `COALESCE(${alias}.${col}, '') AS ${config.aliasColumn}`;
|
||||
return `COALESCE(${alias}.${col}::TEXT, '') AS ${config.aliasColumn}`;
|
||||
} else {
|
||||
return `COALESCE(main.${col}, '') AS ${config.aliasColumn}`;
|
||||
return `COALESCE(main.${col}::TEXT, '') AS ${config.aliasColumn}`;
|
||||
}
|
||||
} else {
|
||||
// 여러 컬럼인 경우 CONCAT으로 연결
|
||||
@@ -205,15 +217,15 @@ export class EntityJoinService {
|
||||
|
||||
if (isJoinTableColumn) {
|
||||
// 조인 테이블 컬럼은 조인 별칭 사용
|
||||
return `COALESCE(${alias}.${col}, '')`;
|
||||
return `COALESCE(${alias}.${col}::TEXT, '')`;
|
||||
} else {
|
||||
// 기본 테이블 컬럼은 main 별칭 사용
|
||||
return `COALESCE(main.${col}, '')`;
|
||||
return `COALESCE(main.${col}::TEXT, '')`;
|
||||
}
|
||||
})
|
||||
.join(`, '${separator}', `);
|
||||
.join(` || '${separator}' || `);
|
||||
|
||||
return `CONCAT(${concatParts}) AS ${config.aliasColumn}`;
|
||||
return `(${concatParts}) AS ${config.aliasColumn}`;
|
||||
}
|
||||
})
|
||||
.join(", ");
|
||||
@@ -336,17 +348,23 @@ export class EntityJoinService {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 참조 컬럼 존재 확인
|
||||
// 참조 컬럼 존재 확인 (displayColumns[0] 사용)
|
||||
const displayColumn = config.displayColumns?.[0] || config.displayColumn;
|
||||
if (!displayColumn) {
|
||||
logger.warn(`표시 컬럼이 설정되지 않음: ${config.sourceColumn}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
const columnExists = await prisma.$queryRaw`
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = ${config.referenceTable}
|
||||
AND column_name = ${config.displayColumn}
|
||||
AND column_name = ${displayColumn}
|
||||
LIMIT 1
|
||||
`;
|
||||
|
||||
if (!Array.isArray(columnExists) || columnExists.length === 0) {
|
||||
logger.warn(
|
||||
`표시 컬럼이 존재하지 않음: ${config.referenceTable}.${config.displayColumn}`
|
||||
`표시 컬럼이 존재하지 않음: ${config.referenceTable}.${displayColumn}`
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2065,21 +2065,27 @@ export class TableManagementService {
|
||||
);
|
||||
|
||||
if (baseJoinConfig) {
|
||||
// joinAlias에서 실제 컬럼명 추출 (예: dept_code_location_name -> location_name)
|
||||
// sourceColumn을 제거한 나머지 부분이 실제 컬럼명
|
||||
const sourceColumn = baseJoinConfig.sourceColumn; // dept_code
|
||||
const joinAlias = additionalColumn.joinAlias; // dept_code_location_name
|
||||
const actualColumnName = joinAlias.replace(`${sourceColumn}_`, ""); // location_name
|
||||
|
||||
// 추가 조인 컬럼 설정 생성
|
||||
const additionalJoinConfig: EntityJoinConfig = {
|
||||
sourceTable: tableName,
|
||||
sourceColumn: baseJoinConfig.sourceColumn, // 원본 컬럼 (writer)
|
||||
referenceTable: additionalColumn.sourceTable, // 참조 테이블 (user_info)
|
||||
referenceColumn: baseJoinConfig.referenceColumn, // 참조 키 (user_id)
|
||||
displayColumns: [additionalColumn.sourceColumn], // 표시할 컬럼들 (email)
|
||||
displayColumn: additionalColumn.sourceColumn, // 하위 호환성
|
||||
aliasColumn: additionalColumn.joinAlias, // 별칭 (writer_email)
|
||||
sourceColumn: baseJoinConfig.sourceColumn, // 원본 컬럼 (dept_code)
|
||||
referenceTable: additionalColumn.sourceTable, // 참조 테이블 (dept_info)
|
||||
referenceColumn: baseJoinConfig.referenceColumn, // 참조 키 (dept_code)
|
||||
displayColumns: [actualColumnName], // 표시할 컬럼들 (location_name)
|
||||
displayColumn: actualColumnName, // 하위 호환성
|
||||
aliasColumn: additionalColumn.joinAlias, // 별칭 (dept_code_location_name)
|
||||
separator: " - ", // 기본 구분자
|
||||
};
|
||||
|
||||
joinConfigs.push(additionalJoinConfig);
|
||||
logger.info(
|
||||
`추가 조인 컬럼 설정 추가: ${additionalJoinConfig.aliasColumn}`
|
||||
`추가 조인 컬럼 설정 추가: ${additionalJoinConfig.aliasColumn} -> ${actualColumnName}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user