Fix deprecation warning in Query.get_or_404() for SQLAlchemy 2.x#1405
Fix deprecation warning in Query.get_or_404() for SQLAlchemy 2.x#1405mreid-tt wants to merge 2 commits intopallets-eco:mainfrom mreid-tt:fix-Query.get_or_404()-deprecation-warning
Query.get_or_404() for SQLAlchemy 2.x#1405Conversation
src/flask_sqlalchemy/query.py
Outdated
| :param description: A custom message to show on the error page. | ||
| """ | ||
| rv = self.get(ident) | ||
| mapper = self._only_full_mapper_zero("get") |
There was a problem hiding this comment.
We should avoid private APIs. Make a request to SQLAlchemy to make this a publicly supported API first.
There was a problem hiding this comment.
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.
|
Wait a second, this issue and PR make no sense. This still is a method on I'd really appreciate if you don't use AI to continue this conversation. |
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. |
|
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. |
|
Also, we've already provided modern methods for quite some time: https://flask-sqlalchemy.readthedocs.io/en/stable/queries/#queries-for-views. |
Description
This pull request updates
Query.get_or_404()to remove the use of the deprecatedQuery.get()method, which triggers aLegacyAPIWarningunder SQLAlchemy 2.x.The change aligns
get_or_404()with the SQLAlchemy 2.x API by using the session’sget()method instead. This preserves existing behavior while ensuring forward compatibility and eliminating the deprecation warning.fixes #1404
Additional Notes
first_or_404()andone_or_404()are unaffected, as they do not use deprecated methods.