# Add Seed Data to a FastAPI SQLModel App

Yes. An agent writes an idempotent seed script for a FastAPI app on SQLModel, fills users and items for local development only, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-seed-data/sqlmodel/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent starts from what the app already seeds. In a project laid out like fastapi/full-stack-fastapi-template, `backend/app/core/db.py` builds the engine with `create_engine(str(settings.DATABASE_URL), pool_pre_ping=True)` and its `init_db` creates exactly one row: the superuser named by `FIRST_SUPERUSER` and `FIRST_SUPERUSER_PASSWORD`, through `crud.create_user`, and only when no user has that email. `backend/app/initial_data.py` calls it, and `backend/scripts/prestart.sh` runs `alembic upgrade head` and then that script, so every environment, including the `test-backend.yml` and `test-docker-compose.yml` workflows and the production `compose.yml`, gets the superuser and nothing else. There is no development data: a fresh checkout shows an empty items list.

Then it reads the models and the tests that touch them. `backend/app/models.py` defines `User` with a UUID primary key, a unique indexed `email`, `hashed_password`, `is_active`, `is_superuser`, `full_name`, `created_at` and an `items` relationship with `cascade_delete=True`, and `Item` with `title`, `description`, `created_at` and an `owner_id` foreign key with `ondelete="CASCADE"`. `crud.py` has `create_user`, which hashes the password with `get_password_hash`, `get_user_by_email` and `create_item`. `backend/tests/conftest.py` opens a session-scoped fixture that calls `init_db` and, when the session ends, deletes every `Item` and every `User`; `tests/utils/user.py` and `item.py` make random rows with `random_lower_string`. So the seed cannot be something the tests rely on, and it must survive being run after them. `core/config.py` has `FASTAPI_ENV`, which is `development` or unset, and `_check_default_secret` already uses it to refuse a `changethis` secret anywhere else.

The script goes in `backend/app/seed.py`, beside `initial_data.py`, and refuses to run unless `settings.FASTAPI_ENV == "development"`, so a copied `prestart.sh` can never seed a deployed database. It holds a fixed list of demo accounts with full names and a few items each, with the demo password read from a `SEED_PASSWORD` environment variable that defaults for local use. For each account it calls `crud.get_user_by_email` and creates the user through `crud.create_user` only when absent, then creates each item through `crud.create_item` only when that owner has no item with that title, so a second run inserts nothing and prints what it skipped. It reuses `init_db` first, so the superuser exists before any demo row. The agent adds a `seed` line to the backend `README.md`'s Local Development section after `uv run bash scripts/prestart.sh`, and a `backend/tests/test_seed.py` that runs the seed twice inside the existing `db` fixture and asserts the user and item counts did not change between the runs; the fixture's teardown deletes the rows afterwards as it does for every other test.

To prove it, the agent installs Postgres with `apt-get` in its sandbox, because the sandbox has no Docker for `compose.yml`, sets `DATABASE_URL` as `.env` shapes it, runs `uv sync`, `uv run bash scripts/prestart.sh`, then `uv run python app/seed.py` twice, and pastes both outputs. It starts `uv run fastapi dev`, signs in as the superuser and requests `GET /api/v1/items/` to show the seeded rows with their `owner_id`s, then runs `uv run bash scripts/test.sh`, `ruff check` and `mypy` as `pyproject.toml` configures them, strict mode included.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add development seed data to this FastAPI app. Read backend/app/models.py, backend/app/crud.py, backend/app/core/db.py, backend/app/core/config.py, backend/app/initial_data.py, backend/scripts/prestart.sh, backend/tests/conftest.py, backend/tests/utils/user.py, backend/tests/utils/item.py and backend/README.md first. Write backend/app/seed.py: refuse to run unless FASTAPI_ENV is development, call init_db first, then create a fixed set of demo users through crud.create_user only when no user has that email, with the password from a SEED_PASSWORD environment variable that defaults for local use, and give each user a few items through crud.create_item only when that owner has no item with that title, printing what was created and what was skipped. Do not change prestart.sh. Add a test in backend/tests/test_seed.py that runs the seed twice inside the existing db fixture and asserts the user and item counts are unchanged between runs, and document the command in backend/README.md under Local Development. Install Postgres in your sandbox, run prestart.sh, run the seed twice and paste both outputs, start the API and show GET /api/v1/items/ as the superuser, then run scripts/test.sh, ruff and mypy and paste their output. Change no model, no migration and no route.",
  "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

The pull request adds `backend/app/seed.py` and `backend/tests/test_seed.py`, and changes `backend/README.md`. Its description lists the demo accounts and items the script creates, shows the first run creating them and the second run skipping every one, and includes the `GET /api/v1/items/` response, the pytest run with coverage, and clean `ruff` and `mypy` output.

`prestart.sh` is untouched, so deployments, the Docker Compose test workflow and CI still get only the superuser; the seed is a command a developer runs once after `prestart.sh`. The script refuses to run when `FASTAPI_ENV` is not `development`, and the pull request shows that refusal's message so a reviewer can see it is a clear stop rather than a silent no-op.

Before merging, a reviewer checks the demo names and items read well in the frontend, decides whether the default demo password is acceptable for local machines or should be required, and confirms the new test leaves the database as it found it alongside the existing fixtures.

## Run it on every Linear issue labelled agent

Add an automation to the Project with this prompt and a `linear` trigger on `linear.issue.labeled` with the label you reserve for the agent in its `labels`. Each labelled issue starts one run with the issue as its context, on its own branch; the trigger adds the issue after the prompt and fills in no placeholder, and a change made by an app integration never fires it, so the agent's own comment on the issue starts nothing.

The automation's `max_runs_per_hour` holds a burst of labels to that many runs an hour: a fire past it is created `queued` and starts when the hour has room, and one past a second hour's worth is skipped as `rate_limited` until the issue is labelled again. From your own server, the same run starts with this request and the issue URL in `resources`, with a `request_id` that is a UUID v5 of the issue id so a retried start returns the run already begun.

## Why run it on Rush?

A seed script is easy to write and easy to get subtly wrong: it runs in production by accident, it fails the second time, or it fights the test fixtures that wipe the tables. The agent reads the init path, the config switch and the test teardown before writing a line, builds the guard and the idempotency in from the start, and proves both with two real runs against a real Postgres in its own sandbox.

The transcript keeps every command with its output, so a reviewer reads the two seed runs, the API response and the test, lint and type checks without checking the branch out. Several services built from the same template can be seeded at once, each run in its own sandbox with its own database, and nothing merges until a person reads the diff.

## Frequently asked questions

### Why not seed inside prestart.sh or init_db?

Both run everywhere: `prestart.sh` is what the production `compose.yml` backend, the CI workflows and the docs tell every environment to run, and `init_db` is called by the test fixture. Demo rows belong only on a developer's machine, so the seed is a separate command with its own `FASTAPI_ENV` check, and `init_db` keeps creating only the superuser.

### Will the seed break the tests?

No. The session fixture in `conftest.py` deletes every item and user when the test session ends, so the seed rows disappear with the random rows the tests make. The new test runs the seed twice inside that fixture, so it also proves the script tolerates a database the tests have already touched.

### Why go through crud.create_user instead of inserting rows?

`create_user` hashes the password with the app's own `get_password_hash` and validates through `UserCreate`, so the seeded accounts can actually sign in and the rows match what the API would have written. A raw insert would need its own hashing and would drift the first time the model changes.

### What if I want more data than a few users?

The script keeps its demo set as a plain list at the top of the file, so adding accounts or items is editing that list; the email and title checks keep it idempotent. For volume testing, ask for a follow-up that generates rows with a seeded random generator and a count flag, kept behind the same `FASTAPI_ENV` guard.

## More SQLModel jobs

- [Add Soft Delete to a SQLModel and FastAPI App](https://getrush.ai/run/add-soft-delete/sqlmodel/)
- [Add updated_at Timestamps to SQLModel Tables](https://getrush.ai/run/add-updated-at-timestamps/sqlmodel/)
- [Migrate a SQLModel and FastAPI App to Async Sessions](https://getrush.ai/run/migrate-to-async/sqlmodel/)

## The same job on other stacks

- [Write a Seed File for a Supabase Project](https://getrush.ai/run/add-seed-data/supabase/)
- [Add Seed Data to a NestJS TypeORM App](https://getrush.ai/run/add-seed-data/typeorm/)
- [Add Seed Data to a Vapor App](https://getrush.ai/run/add-seed-data/vapor/)

## 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/)
