Fix bulk delete foreign key constraints and bulk upload phone unique constraint

This commit is contained in:
2026-09-25 10:54:18 +05:30
parent f1dcf1979d
commit e12c3fb8eb

View File

@@ -231,7 +231,7 @@ def change_password(payload: ChangePasswordRequest, session: Session = Depends(g
async def bulk_register_students(file: UploadFile = File(...), session: Session = Depends(get_session)):
# Simple check for now (we can rely on frontend calling it correctly or auth middleware if we add it)
content = await file.read()
decoded = content.decode('utf-8')
decoded = content.decode('utf-8-sig')
csv_reader = csv.DictReader(io.StringIO(decoded))
results = []
@@ -267,7 +267,7 @@ async def bulk_register_students(file: UploadFile = File(...), session: Session
user = models.User(
email=email,
name=name,
phone=phone,
phone=phone if phone else None,
password_hash=hashed_password,
target_exam=exam,
tier=tier,
@@ -297,6 +297,9 @@ def bulk_delete_students(payload: DeleteUsersRequest, session: Session = Depends
# Delete associated records manually since foreign key cascade might not be configured in sqlmodel by default
session.exec(delete(models.Attempt).where(models.Attempt.user_id == uid))
session.exec(delete(models.Mistake).where(models.Mistake.user_id == uid))
session.exec(delete(models.Transaction).where(models.Transaction.user_id == uid))
session.exec(delete(models.Subscription).where(models.Subscription.user_id == uid))
session.exec(delete(models.StudyPlan).where(models.StudyPlan.user_id == uid))
session.delete(user)
session.commit()
return {"success": True}