From b7f41e85961916a4525a36de5d470069f474f97a Mon Sep 17 00:00:00 2001 From: lvrossem Date: Tue, 28 Mar 2023 13:01:20 -0600 Subject: [PATCH] Working get request for users --- src/models.py | 20 +++++++++++++++----- src/schemas/highscores.py | 7 +------ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/models.py b/src/models.py index ffc5f60..898c2cd 100644 --- a/src/models.py +++ b/src/models.py @@ -1,5 +1,6 @@ from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, Float from sqlalchemy.orm import relationship +from sqlalchemy.dialects.postgresql import ARRAY from enum import Enum from enums import MinigameEnum, CourseEnum, StrEnumType @@ -13,8 +14,14 @@ class User(Base): username = Column(String, unique=True, index=True, nullable=False) hashed_password = Column(String, nullable=False) - high_scores = relationship("HighScore", back_populates="owner") - course_progresses = relationship("CourseProgress", back_populates="owner") + high_scores = relationship("HighScore", back_populates="owner", cascade="all, delete", lazy="dynamic") + course_progress = relationship("CourseProgress", back_populates="owner", cascade="all, delete", lazy="dynamic") + + # add a new column to store the high_score IDs + high_score_ids = Column(ARRAY(Integer), default=[]) + + # add a new column to store the course_progress IDs + course_progress_ids = Column(ARRAY(Integer), default=[]) class HighScore(Base): @@ -23,7 +30,9 @@ class HighScore(Base): high_score_id = Column(Integer, primary_key=True, index=True) score_value = Column(Float, nullable=False) minigame = Column(StrEnumType(MinigameEnum), nullable=False) - owner = Column(Integer, ForeignKey("users.user_id")) + owner_id = Column(Integer, ForeignKey("users.user_id")) + owner = relationship("User", back_populates="high_scores") + class CourseProgress(Base): @@ -32,5 +41,6 @@ class CourseProgress(Base): course_progress_id = Column(Integer, primary_key=True, index=True) progress_value = Column(Float, nullable=False) course = Column(StrEnumType(CourseEnum), nullable=False) - owner = Column(Integer, ForeignKey("users.user_id")) - + owner_id = Column(Integer, ForeignKey("users.user_id")) + owner = relationship("User", back_populates="course_progress") + diff --git a/src/schemas/highscores.py b/src/schemas/highscores.py index 3cf35af..726e647 100644 --- a/src/schemas/highscores.py +++ b/src/schemas/highscores.py @@ -6,12 +6,7 @@ class HighScore(BaseModel): high_score_id: int score_value: float minigame: MinigameEnum - owner_id: "User" + owner_id: int class Config: orm_mode = True - - -# It's ugly, but I have no choice -from users import User -HighScore.update_forward_refs() \ No newline at end of file