Skip to content

add abort methods - #31

Merged
davidism merged 1 commit into
mainfrom
abort
Nov 6, 2025
Merged

add abort methods#31
davidism merged 1 commit into
mainfrom
abort

Conversation

@davidism

@davidism davidism commented Nov 6, 2025

Copy link
Copy Markdown
Member

Add get_or_abort and one_or_abort methods (and async variants), which will return a single row or call flask.abort(404). These are similar to get_or_404 etc from Flask-SQLAlchemy. They take arguments to customize the query execution as well as the abort call.

I decided not to port first_or_404 because these methods seem intended for detail pages, where first would not catch that the result may not be unique. one_or_abort covers that case.

get will always return a model instance, but one works on any select statement, which returns a tuple result. I added a scalar argument to control whether to return a single item or the tuple result. It defaults to true since the common use case will be to query a model rather than a tuple of columns. It's possible to introspect the select statement to determine whether a single model is being queried, but I felt explicitly passing scalar=False was preferable.

For the first release of Flask-SQLAlchemy-Lite, I wanted to keep it as simple as possible, waiting to see what may be needed from Flask-SQLAlchemy. These helper methods handle a really common pattern in web applications, and go beyond just tweaking SQLAlchemy to actually integrate with Flask. So I felt it was appropriate to have these in this "light" extension.

@davidism davidism added this to the 0.2.0 milestone Nov 6, 2025
@davidism
davidism merged commit d5c0277 into main Nov 6, 2025
9 checks passed
@davidism
davidism deleted the abort branch November 6, 2025 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant