## TL;DR
Autogenerate diffs your models against whatever database it connects to, so a database carrying another branch's schema produces operations for someone else's change. Review every operation in the generated file, delete the ones that are not yours, and regenerate against a database at the true baseline when the noise is heavy. Never commit unaudited autogenerate output.

## The query

```text
agent generated an alembic migration with autogenerate and it included unrelated schema noise from another branch
```

## Use this when

- alembic autogenerate included unrelated schema changes
- Generated migration has operations from another branch
- Autogenerate diff is noisy
- An agent committed autogenerate output without reviewing it

## Not for

- Autogenerate missing an expected change (check the model metadata and include_object)
- Hand-written migrations with wrong operations (a different review problem)
- Autogenerate failing to connect (a config problem)

## Steps

### 1. Read the generated file operation by operation

Open the revision file and read every op.* call. For each one, ask: does this correspond to a model change I made? Mark the ones that do not.

Expected output: the file split into "mine" and "noise". The noise usually clusters: another branch's table, an index someone else added, a type change from a different feature.

### 2. Check what database it diffed against

```bash
grep -n "sqlalchemy.url" alembic.ini
```

Confirm which database autogenerate compared your models to. If it was a shared dev database carrying other branches' migrations, that is the noise source.

Expected output: the database identity, and whether it sits at your branch's baseline or somewhere else.

### 3. Delete the noise, keep yours

Remove the unrelated operations from the file by hand, keeping the revision id and down_revision intact. Re-read the remainder: it should describe exactly your change and nothing else.

Expected output: a migration file containing only your operations.

### 4. For heavy noise: regenerate on a clean baseline

Build a scratch database at your branch's true parent revision, point alembic at it, and regenerate:

```bash
alembic revision --autogenerate -m "[your change]"
```

Expected output: a clean file with only your operations, because the baseline database has no other branch's schema in it.

### 5. Make the agent review autogenerate output as a rule

The agent's migration procedure: autogenerate output is a draft, never a commit. Every operation gets a one-line justification before the file is kept. Run autogenerate only against a known-baseline database.

Expected output: noisy diffs get caught at generation time, not in code review.

## Tool compatibility

alembic 1.x with sqlalchemy 1.4/2.0. Autogenerate diffing behavior is the same.

## Variant phrasings

### alembic autogenerate unrelated schema changes

Steps 1 through 3: audit the file, delete the noise.

### autogenerate noise from another branch

Step 4: regenerate against a database at your branch's true baseline.

### alembic revision autogenerate review

Step 5: treat autogenerate output as a draft requiring per-operation justification.

## Why it happens

Autogenerate has no notion of "your change": it computes the symmetric difference between model metadata and the connected database. Any schema the database has that the models lack (another branch's migration, a manual tweak, a leftover experiment) becomes a drop or alter in your file. The database you diff against is an input to the generation, and a dirty input makes a dirty file.

## Edge cases

- Deleting noise by hand risks deleting a real operation: when in doubt, regenerate on the clean baseline instead of editing.
- Autogenerate cannot detect some changes at all (column renames look like drop+add, some type changes are invisible): the review step catches what the diff misses, in both directions.
- Shared dev databases are the root cause in most shops: give autogenerate a personal scratch database and the noise class disappears.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_Bb0U5tldDqffm3osaIab8A
