From 279a90bb60eb00b533852ff71f62b9bfe436e83d Mon Sep 17 00:00:00 2001 From: miboelae Date: Thu, 23 May 2024 14:01:01 +0200 Subject: [PATCH] fixes remove user cascading on groups/subjects/... --- backend/src/subject/models.py | 4 ++-- backend/src/user/service.py | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/backend/src/subject/models.py b/backend/src/subject/models.py index cf257a82..a59d0f29 100644 --- a/backend/src/subject/models.py +++ b/backend/src/subject/models.py @@ -7,8 +7,8 @@ StudentSubject = Table( "student_subject", Base.metadata, - Column("uid", ForeignKey("website_user.uid"), primary_key=True), - Column("subject_id", ForeignKey("subject.id"), primary_key=True), + Column("uid", ForeignKey("website_user.uid", ondelete="CASCADE"), primary_key=True), + Column("subject_id", ForeignKey("subject.id" , ondelete="CASCADE"), primary_key=True), ) InstructorSubject = Table( diff --git a/backend/src/user/service.py b/backend/src/user/service.py index 19eb8c54..5c0c3399 100644 --- a/backend/src/user/service.py +++ b/backend/src/user/service.py @@ -1,11 +1,13 @@ from typing import Sequence -from sqlalchemy import select +from sqlalchemy import select, delete from sqlalchemy.ext.asyncio import AsyncSession from .models import User from .schemas import UserCreate -from ..subject.models import InstructorSubject +from ..subject.models import InstructorSubject, StudentSubject, Subject +from ..group.models import StudentGroup +from ..project.models import InstructorProject async def get_by_id(db: AsyncSession, user_id: str) -> User: @@ -39,7 +41,14 @@ async def set_teacher(db: AsyncSession, user_id: str, value: bool): async def delete_user(db: AsyncSession, user_id: str): user = await get_by_id(db, user_id) - await db.delete(user) + if user: + await db.execute(delete(StudentSubject).where(StudentSubject.c.uid == user_id)) + await db.execute(delete(InstructorProject).where(InstructorProject.c.uid == user_id)) + await db.execute(delete(StudentGroup).where(StudentGroup.c.uid == user_id)) + await db.flush() + + await db.delete(user) + await db.commit() async def get_instructors(db: AsyncSession) -> Sequence[User]: