fix: resolve machine deletion failure after parts removal by nullifying orphaned FK references
All checks were successful
Deploy to Production / deploy (push) Successful in 46s
All checks were successful
Deploy to Production / deploy (push) Successful in 46s
This commit is contained in:
@@ -3,12 +3,18 @@ from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Depends, Path
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import select, func
|
||||
from sqlalchemy import select, func, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from src.database.config import get_db
|
||||
from src.database.models import Machine, EquipmentPart
|
||||
from src.database.models import (
|
||||
Machine,
|
||||
EquipmentPart,
|
||||
InspectionTemplate,
|
||||
InspectionTemplateItem,
|
||||
Alarm,
|
||||
)
|
||||
from src.auth.models import TokenData
|
||||
from src.auth.dependencies import require_auth, verify_tenant_access
|
||||
|
||||
@@ -288,6 +294,29 @@ async def delete_machine(
|
||||
detail=f"활성 부품이 {parts_count}개 있어 삭제할 수 없습니다. 먼저 부품을 제거해주세요.",
|
||||
)
|
||||
|
||||
# Null out FKs in tables without cascade before deleting machine
|
||||
part_ids_stmt = select(EquipmentPart.id).where(
|
||||
EquipmentPart.machine_id == machine_id
|
||||
)
|
||||
await db.execute(
|
||||
update(InspectionTemplateItem)
|
||||
.where(InspectionTemplateItem.equipment_part_id.in_(part_ids_stmt))
|
||||
.values(equipment_part_id=None)
|
||||
)
|
||||
await db.execute(
|
||||
update(Alarm)
|
||||
.where(Alarm.equipment_part_id.in_(part_ids_stmt))
|
||||
.values(equipment_part_id=None)
|
||||
)
|
||||
await db.execute(
|
||||
update(InspectionTemplate)
|
||||
.where(InspectionTemplate.machine_id == machine_id)
|
||||
.values(machine_id=None)
|
||||
)
|
||||
await db.execute(
|
||||
update(Alarm).where(Alarm.machine_id == machine_id).values(machine_id=None)
|
||||
)
|
||||
|
||||
await db.delete(machine)
|
||||
await db.commit()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user