From 48a07673e49cadada1ac380bf7c4246caddd2a2c Mon Sep 17 00:00:00 2001 From: ComputasAlex Date: Tue, 9 Jul 2024 13:59:37 +0200 Subject: [PATCH] Fixes database error upon deleting old records. Changes to > 1 day old because of highscore --- src/webapp/models.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/webapp/models.py b/src/webapp/models.py index 065fb5c..0e41a02 100644 --- a/src/webapp/models.py +++ b/src/webapp/models.py @@ -50,7 +50,8 @@ class Scores(db.Model): """ score_id = db.Column(db.Integer, primary_key=True, autoincrement=True) - player_id = db.Column(db.NVARCHAR(32), db.ForeignKey("players.player_id")) + player_id = db.Column(db.NVARCHAR(32), db.ForeignKey( + "players.player_id", ondelete='CASCADE')) score = db.Column(db.Integer, nullable=False) date = db.Column(db.Date) difficulty_id = db.Column( @@ -69,6 +70,7 @@ class Players(db.Model): state = db.Column(db.String(32), nullable=False) game = db.relationship("Games", back_populates="players") + scores = db.relationship("Scores", backref="Players", passive_deletes=True) class MulitPlayer(db.Model): @@ -382,14 +384,14 @@ def delete_session_from_game(game_id): def delete_old_games(): """ - Delete records in games older than one hour. + Delete records in games older than one day. """ try: games = ( db.session.query(Games) .filter( Games.date - < (datetime.datetime.today() - datetime.timedelta(hours=1)) + < (datetime.datetime.today() - datetime.timedelta(days=1)) ) .all() )