Skip to content

feat: add find_or_fail method to Model and QueryBuilder - #59

Merged
tmgbedu merged 7 commits into
mainfrom
feat/find-or-fail
May 21, 2026
Merged

feat: add find_or_fail method to Model and QueryBuilder#59
tmgbedu merged 7 commits into
mainfrom
feat/find-or-fail

Conversation

@tmgbedu

@tmgbedu tmgbedu commented May 21, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Added find_or_fail(primary_key, columns=None) to QueryBuilder — raises ModelNotFoundException (HTTP 404) when no record matches the given primary key
  • Added find_or_fail classmethod to Model that delegates to the builder
  • Updated ModelNotFoundException to accept an optional message in __init__, so the error message includes the model name and key value

Usage

# Raises ModelNotFoundException if user with id=42 doesn't exist
user = await User.find_or_fail(42)

Test plan

  • Call await SomeModel.find_or_fail(existing_id) — should return the model instance
  • Call await SomeModel.find_or_fail(nonexistent_id) — should raise ModelNotFoundException with a 404 status
  • Verify the exception message includes the model name and primary key

🤖 Generated with Claude Code

tmgbedu and others added 3 commits May 21, 2026 15:17
Raises ModelNotFoundException (404) when no record matches the given
primary key, mirroring the Laravel/Masonite find_or_fail convention.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Covers: returns model when found, raises ModelNotFoundException when not
found, exception has 404 status, and exception message contains the key.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@tmgbedu
tmgbedu force-pushed the feat/find-or-fail branch from f90abd4 to 1f40e4a Compare May 21, 2026 22:36
tmgbedu and others added 4 commits May 21, 2026 15:41
These belong in their own dedicated PR.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add fastapi_startkit/masoniteorm/exceptions.py as the canonical home
- Export ModelNotFoundException from fastapi_startkit.masoniteorm
- Keep a re-export in fastapi_startkit.exceptions for backwards compatibility

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@tmgbedu
tmgbedu merged commit ff07a7e into main May 21, 2026
3 checks passed
@tmgbedu
tmgbedu deleted the feat/find-or-fail branch May 22, 2026 04:53
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.

1 participant