# Fix "LegacyAPIWarning: The Query.get() method is considered legacy as of the 1.x series of SQLAlchemy"

Yes. An agent replaces Model.query.get() with db.session.get(), shows which commands surface the warning, and drives the route with a test client before and after.

[Run this on Rush](https://cloud.getrush.ai/run/fix-query-get-legacy-api-warning/flask-sqlalchemy/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the app and how it is installed, because this warning only appears under some commands and the fix should be checked under the one that raised it. In a small Flask app like yymm/flask-vuejs, `requirements.txt` lists `Flask`, `Flask-SQLAlchemy` and `gunicorn` with no versions, the `Procfile` runs `gunicorn app:app`, and `db.cfg` points `SQLALCHEMY_DATABASE_URI` at `sqlite:///test.db`. `app.py` declares one model, `Todo` on the `todos` table with a `todo_id` primary key, `title`, `done` and `pub_date`, and four JSON routes; `/sqlalchemy/update` at line 96 fetches the row with `Todo.query.get(request.json['id'])` before setting `done` and `title` and committing.

Then it reproduces, which takes some care. A clean install today resolves Flask 3.1.3, Flask-SQLAlchemy 3.1.1 and SQLAlchemy 2.1.8. Under a plain `python`, a test client that creates the table, adds a row and posts to `/sqlalchemy/update` prints nothing, because `LegacyAPIWarning` descends from `DeprecationWarning` and Python hides those outside `__main__`. With `python -W always` the line appears: `app.py:96: LegacyAPIWarning: The Query.get() method is considered legacy as of the 1.x series of SQLAlchemy and becomes a legacy construct in 2.0. The method is now available as Session.get() (deprecated since: 2.0) (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)`. Under pytest the same line lands in the warnings summary of an otherwise passing test, and with `-W error::DeprecationWarning` the request fails with the warning as an exception.

The fix is `db.session.get(Todo, request.json['id'])`, the `Session.get()` the warning names, reached through Flask-SQLAlchemy's scoped session. The other three routes use `Todo.query.order_by(...).all()` and `db.session.add`, which the warning does not cover, so they stay. The agent also notes the `/sqlalchemy` route's bare `except:` that creates the database on first failure, and leaves it alone as unrelated.

Proof is the same test client run twice. After the change, `GET /sqlalchemy/get` lists the row, `POST /sqlalchemy/update` returns `{"status": "ok"}` and the next `GET` shows `done` true with the new title, and `python -W always` prints no `LegacyAPIWarning`. The pytest run passes with an empty warnings summary.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Our test run reports \"LegacyAPIWarning: The Query.get() method is considered legacy as of the 1.x series of SQLAlchemy\". Find every .query.get( call in the app, say which Flask, Flask-SQLAlchemy and SQLAlchemy versions a clean install resolves, and show under which commands the warning is visible (plain python, python -W always, pytest, -W error). Replace each call with db.session.get(Model, id), then drive the affected routes with Flask's test client against a fresh SQLite file before and after, and open a pull request with the version list, the warning as printed and both transcripts.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that changes one line in `app.py`: `Todo.query.get(...)` becomes `db.session.get(Todo, ...)`. The description names the three resolved versions, quotes the warning with its file and line, explains why a plain `python` run hides it and `pytest` shows it, and includes the before-and-after test client transcript with the updated row.

A reviewer checks that the lookup is by primary key as before, that the commit still happens, and that no other `query.get` remains. They see that `Todo.query.order_by(...).all()` and `db.session.add` were left as they are because the warning does not name them.

The description lists the two things the agent saw and did not change: the bare `except:` that creates the database on first request, and the unpinned `requirements.txt`.

## Run it on every pytest run with a warnings summary

The warning appears in CI test logs long before it fails anything, so the test job is the trigger. A step in the workflow posts the pytest output to a Rush `webhook` trigger with `Authorization: Bearer` and the trigger's secret when the warnings summary contains `LegacyAPIWarning`, and the log follows the prompt as data; the run opens the pull request with the replacement calls.

For a set of Flask services that share the same pattern, your server sends this request once per repository, each with its own UUID as `request_id` so a retry returns the run already started, and reads each `run.completed` webhook; a key starts at most `max_concurrent` runs at once and queues the rest.

## Why run it on Rush?

A warning that only some runners show is easy to dismiss as noise, until a `-W error` flag or a pytest configuration turns it into a failure. In a Rush sandbox the agent runs the app under each command, keeps every transcript, and names the resolved versions, so the pull request explains why the warning comes and goes rather than only silencing it.

The change is one line, but it touches a route that writes to the database. The agent drives the route with a test client against a fresh SQLite file before and after and shows the row updated, so the reviewer reads a diff with the behaviour proven, not assumed.

## Frequently asked questions

### Why do I see this under pytest but not when running the app?

Because `LegacyAPIWarning` is a `DeprecationWarning`, which Python hides by default outside the main module. pytest shows deprecation warnings in its summary, and `python -W always` or `-W error::DeprecationWarning` make them visible or fatal.

### What is the Flask-SQLAlchemy way to write Session.get()?

`db.session.get(Model, primary_key)`. It returns the instance or `None`, exactly like `Model.query.get()` did, so the surrounding code that sets fields and commits does not change.

### Do Model.query.all() and order_by() warn too?

Not in this run: the three other routes use `Todo.query.order_by(...).all()` and the warning names only `Query.get()`. The agent reports what the run printed and changes only the call that warned.

### Should the requirements be pinned at the same time?

That is a separate decision. The pull request records the versions the clean install resolved, Flask 3.1.3, Flask-SQLAlchemy 3.1.1 and SQLAlchemy 2.1.8, so the maintainers can pin them if they want the same result on the next machine.

## More Flask-SQLAlchemy jobs

- [Fix "RuntimeError: Working outside of application context" from db.create_all() in Flask-SQLAlchemy 3](https://getrush.ai/run/fix-working-outside-of-application-context/flask-sqlalchemy/)
- [Fix "AttributeError: 'Flask' object has no attribute 'before_first_request'" with Flask-SQLAlchemy](https://getrush.ai/run/fix-before-first-request-attribute-error/flask-sqlalchemy/)

## More jobs like this: Fix a bug from an alert

- [Fix Quoted References Are Deprecated Warnings in a Terraform AWS Module](https://getrush.ai/run/fix-quoted-references-are-deprecated-warning/terraform/)
- [Fix "rand.Seed has been deprecated since Go 1.20" (SA1019) in a Go Program](https://getrush.ai/run/fix-rand-seed-deprecated/math-rand/)
- [Fix "TypeError: Random.shuffle() takes 2 positional arguments but 3 were given" on Python 3.11](https://getrush.ai/run/fix-random-shuffle-takes-2-positional-arguments/random/)
- [Fix "ReactDOM.render is no longer supported in React 18. Use createRoot instead"](https://getrush.ai/run/fix-reactdom-render-no-longer-supported/react/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
