Skip to content

Commit

Permalink
fixes remove user cascading on groups/subjects/...
Browse files Browse the repository at this point in the history
  • Loading branch information
miboelae committed May 23, 2024
1 parent 75ee5aa commit 279a90b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions backend/src/subject/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 12 additions & 3 deletions backend/src/user/service.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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]:
Expand Down

0 comments on commit 279a90b

Please sign in to comment.