Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Workaround fix for the session's unique_together

Roland Geider0779f7ec693d
diff --git a/wger/manager/models/log.py b/wger/manager/models/log.py
index dea484963..21b805adf 100644
--- a/wger/manager/models/log.py
+++ b/wger/manager/models/log.py
@@ -14,10 +14,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-# Standard Library
-import datetime
-from uuid import uuid4
-
# Django
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
@@ -275,11 +271,28 @@ class WorkoutLog(models.Model):
# Auto-create a session only if the client didn't provide one.
if not self.session_id:
- self.session = WorkoutSession.objects.get_or_create(
- user=self.user,
- date=self.date,
- routine=self.routine,
- )[0]
+ try:
+ self.session = WorkoutSession.objects.get_or_create(
+ user=self.user,
+ date=self.date,
+ routine=self.routine,
+ )[0]
+ except WorkoutSession.MultipleObjectsReturned:
+ # TODO: duplicate sessions can exist for the same (user, date, routine)
+ # when routine is NULL, as the unique_together does not cover a NULL
+ # routine in PostgreSQL.
+ # This is a fix till we correctly take care of the problem, we just
+ # reuse one session (ids are uuid7, so ordering by id yields the
+ # earliest) instead of crashing the log POST with MultipleObjectsReturned.
+ self.session = (
+ WorkoutSession.objects.filter(
+ user=self.user,
+ date=self.date,
+ routine=self.routine,
+ )
+ .order_by('id')
+ .first()
+ )
# If the user of next_log is not this user, remove foreign key
if self.next_log and self.next_log.user != self.user:
diff --git a/wger/manager/tests/test_log_model.py b/wger/manager/tests/test_log_model.py
index 4f5a5dcf7..b934a6fc4 100644
--- a/wger/manager/tests/test_log_model.py
+++ b/wger/manager/tests/test_log_model.py
@@ -50,6 +50,34 @@ class LogModelTestCase(WgerTestCase):
self.assertEqual(WorkoutSession.objects.count(), 1)
+ def test_save_reuses_session_when_duplicates_exist(self):
+ """
+ Duplicate routine-less sessions can exist for one (user, date) because the
+ unique_together does not cover a NULL routine. A new log's save() must
+ reuse an existing session instead of raising MultipleObjectsReturned.
+ """
+ WorkoutLog.objects.all().delete()
+ WorkoutSession.objects.all().delete()
+
+ # First log auto-creates a routine-less session for today.
+ WorkoutLog(user_id=1, exercise_id=1, weight=10, repetitions=10).save()
+ session = WorkoutSession.objects.get()
+ self.assertIsNone(session.routine_id)
+
+ # A duplicate (user, date, routine=None) — only possible because a NULL
+ # routine escapes the unique_together guard.
+ WorkoutSession.objects.create(user_id=1, date=session.date, routine=None)
+ self.assertEqual(WorkoutSession.objects.count(), 2)
+
+ # A second log for the same day must not crash and must not add a session.
+ log = WorkoutLog(user_id=1, exercise_id=1, weight=20, repetitions=8)
+ log.save()
+
+ log.refresh_from_db()
+ self.assertEqual(WorkoutSession.objects.count(), 2)
+ existing_ids = set(WorkoutSession.objects.values_list('id', flat=True))
+ self.assertIn(log.session_id, existing_ids)
+
def test_dont_create_session_when_already_set(self):
"""
If the log already has a (valid, own) session, the auto-create magic must