Skip to content

fix: prevent PGRST116 crash in getTodayActivities by adding limit(1) before maybeSingle#168

Merged
jddeep merged 2 commits intoAOSSIE-Org:mainfrom
Varadraj75:fix/today-activities-maybeSingle-crash
Mar 1, 2026
Merged

fix: prevent PGRST116 crash in getTodayActivities by adding limit(1) before maybeSingle#168
jddeep merged 2 commits intoAOSSIE-Org:mainfrom
Varadraj75:fix/today-activities-maybeSingle-crash

Conversation

@Varadraj75
Copy link
Copy Markdown
Contributor

@Varadraj75 Varadraj75 commented Feb 27, 2026

Closes #167

📝 Description

getTodayActivities was calling .maybeSingle() directly after a date
range filter on daily_activity_logs. If more than one log existed for
the same patient on the same day, Supabase would throw a PGRST116 error
("Results contain more than one row"), causing the tasks page to silently
show "No activities found".

🔧 Changes Made

  • patient/lib/repository/supabase_patient_repository.dart: Added
    .order('date', ascending: false) and .limit(1) before .maybeSingle()
    to ensure at most one row is returned, always picking the most recent log.

📷 Screenshots or Visual Changes (if applicable)

N/A — error only occurs when duplicate logs exist in the DB.

🤝 Collaboration

Collaborated with: N/A

✅ Checklist

  • I have read the contributing guidelines.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added necessary documentation (if applicable).
  • Any dependent changes have been merged and published in downstream modules.

Summary by CodeRabbit

  • Bug Fixes
    • Improved patient activity log retrieval to return only the most recent activity entry for the current day, ensuring consistent and accurate activity data display.

Copilot AI review requested due to automatic review settings February 27, 2026 11:39
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Feb 27, 2026

Warning

Rate limit exceeded

@Varadraj75 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 16 minutes and 56 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 3e8ac41 and 0f38e3f.

📒 Files selected for processing (1)
  • patient/lib/repository/supabase_patient_repository.dart
📝 Walkthrough

Walkthrough

A query modification in getTodayActivities now chains .order('date', ascending: false) and .limit(1) to retrieve only the most recent daily activity log for a given day, preventing crashes when duplicate logs exist for the same date.

Changes

Cohort / File(s) Summary
Query Optimization for Duplicate Handling
patient/lib/repository/supabase_patient_repository.dart
Added .order('date', ascending: false) and .limit(1) to the daily_activity_logs query in getTodayActivities to ensure a single, most-recent result is returned when multiple logs exist for the same day, preventing PGRST116 errors from .maybeSingle().

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~4 minutes

Possibly related PRs

  • PR #125: Modifies the date filtering logic in getTodayActivities, potentially working in conjunction with this query refinement.
  • PR #116: Introduced the original getTodayActivities method that this change refines.

Poem

🐰 A query once crashed with a PGRST116 cry,
Too many logs on one day made activities die.
But order and limit now dance hand in hand,
Fetching the freshest, the most recent and grand!
No more duplicates causing dismay,
Today's activities shine bright every day! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: preventing a PGRST116 crash by adding limit(1) before maybeSingle in getTodayActivities.
Linked Issues check ✅ Passed The code changes directly address issue #167 by adding .order('date', ascending: false) and .limit(1) before .maybeSingle(), matching the suggested fix exactly.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the PGRST116 crash in getTodayActivities as specified in issue #167; no unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes getTodayActivities to avoid Supabase/PostgREST PGRST116 (“Results contain more than one row”) when duplicate daily_activity_logs exist for the same patient/day, by ensuring the query returns at most one row.

Changes:

  • Add deterministic selection logic to the daily_activity_logs query by sorting and limiting before calling .maybeSingle().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread patient/lib/repository/supabase_patient_repository.dart Outdated
Comment thread patient/lib/repository/supabase_patient_repository.dart
@jddeep jddeep merged commit 421e7c2 into AOSSIE-Org:main Mar 1, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: getTodayActivities crashes with PGRST116 when duplicate daily_activity_logs exist

3 participants