Skip to content

Conversation

@mreid-tt
Copy link

Description

This pull request updates Query.get_or_404() to remove the use of the deprecated Query.get() method, which triggers a LegacyAPIWarning under SQLAlchemy 2.x.

The change aligns get_or_404() with the SQLAlchemy 2.x API by using the session’s get() method instead. This preserves existing behavior while ensuring forward compatibility and eliminating the deprecation warning.

fixes #1404

Additional Notes

  • No behavioral changes are introduced beyond internal API alignment.
  • first_or_404() and one_or_404() are unaffected, as they do not use deprecated methods.

:param description: A custom message to show on the error page.
"""
rv = self.get(ident)
mapper = self._only_full_mapper_zero("get")
Copy link
Member

Choose a reason for hiding this comment

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

We should avoid private APIs. Make a request to SQLAlchemy to make this a publicly supported API first.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for catching that! I’ve updated the implementation to use the public API instead of the private one. It now relies on column_descriptions to resolve the mapped class.

@davidism
Copy link
Member

davidism commented Nov 1, 2025

Wait a second, this issue and PR make no sense. This still is a method on Query, which is the legacy API. Using the new select API from within it makes no sense, just don't use query to begin with in that case.

I'd really appreciate if you don't use AI to continue this conversation.

@mreid-tt
Copy link
Author

mreid-tt commented Nov 1, 2025

Wait a second, this issue and PR make no sense. This still is a method on Query, which is the legacy API. Using the new select API from within it makes no sense, just don't use query to begin with in that case.

To be honest, I’m not a Python expert; I was mainly frustrated by the repeated deprecation warnings. Since the repository hasn’t seen much recent maintenance, I decided to try a small fix. The goal wasn’t a full refactor, just a minimal change to remove the warnings and hopefully support a minor release, given that it’s been about two years since the last one.

@davidism davidism closed this Nov 1, 2025
@davidism
Copy link
Member

davidism commented Nov 1, 2025

Going to close this then. I'd recommend using flask-sqlalchemy-lite and moving to use the sqlalchemy select api directly, if all you want to do is use modern sqlalchemy without warnings.

@mreid-tt mreid-tt deleted the fix-Query.get_or_404()-deprecation-warning branch November 1, 2025 01:28
@davidism
Copy link
Member

davidism commented Nov 1, 2025

Also, we've already provided modern methods for quite some time: https://flask-sqlalchemy.readthedocs.io/en/stable/queries/#queries-for-views.

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.

Query.get_or_404() triggers deprecation warning under SQLAlchemy 2.x

2 participants