SQLModel Update Existing Record in FastAPI: PATCH with exclude_unset
A partial request wiped your stored fields to None. Or your update model insists on every field. Or .dict() or .model_dump() throws an AttributeError. One PATCH flow, shown below, fixes all three.
The short answer
As of November 2026, the setup is this. Define a separate HeroUpdate model with every field optional, and expose it through a PATCH endpoint. Load the row with session.get(Hero, hero_id), then copy over only the fields the client sent with exclude_unset=True. Without that flag, skipped fields come back as None and overwrite your stored data.
db_hero.sqlmodel_update(hero.model_dump(exclude_unset=True))
There’s also a version difference in the method name. SQLModel 0.0.14 and later uses model_dump, and earlier versions use dict. If your code calls the name that belongs to the other version, the attribute may not exist on your model.
I’ll stick to the tutorial’s Hero model and assume you already have a SessionDep session dependency. Swap in your own fields.
Why partial updates overwrite your data
It’s a shape mismatch. A PATCH body describes a few fields, but HeroUpdate has a slot for all three of name, secret_name and age. Every one of them is optional so a client can send only some, which is the whole point of PATCH.

Say a request sends only {"age": 16}. The model still holds name=None and secret_name=None, and nothing in it records that those two were never sent. Call a plain hero.model_dump(), copy the result onto the row, and the stored values are replaced with None.
The filter has to run before the data touches the row. exclude_unset=True does that by keeping only what the client actually sent.
The “every field is required” error has a different source. HeroBase has required fields with no default values, so a HeroUpdate that inherits from it makes every field required on a PATCH request.
The fix, step by step

1. Write HeroUpdate from scratch
Don’t inherit from HeroBase, because its required fields would come along with it. Write a standalone model where every field is optional:
class HeroUpdate(SQLModel):
name: str | None = None
secret_name: str | None = None
age: int | None = None
2. Expose it through PATCH, not PUT
You’re changing only part of a row, so use PATCH. If the client sends only some of a row’s fields, that’s a PATCH. The SQLModel tutorial picks it for exactly that reason: it partially updates data.
The body type is HeroUpdate, the id comes from the path, and SessionDep is your existing session dependency. The full endpoint is at the end of step 6.
3. Load the row and return 404 if it’s missing
Read the record with session.get(Hero, hero_id). If it returns nothing, raise a 404 with the detail “Hero not found” before you touch the payload. There’s nothing to update on a missing row.
4. Extract only the fields the client sent
This is the step that causes most of the trouble. Plain model_dump() returns every field, and the ones the client skipped come back as None. Passing exclude_unset=True drops them.
The method name depends on your SQLModel version, so check what’s installed before you copy anything:
- Before 0.0.14:
hero.dict(exclude_unset=True) - 0.0.14 and later:
hero.model_dump(exclude_unset=True)
The rename to model_dump was made for consistency with Pydantic v2. If you’re on 0.0.14 or later, use model_dump and don’t look back.
Leave the flag off and a stored value resets to None after the next partial request. So if you’re seeing None where data used to be, check the hero_data line first. It should read hero.model_dump(exclude_unset=True), or hero.dict(exclude_unset=True) on SQLModel before 0.0.14.
5. Apply the changes to the row you loaded
Pass the dictionary to sqlmodel_update() on the existing object: db_hero.sqlmodel_update(hero_data). This copies the client’s values onto the row. Fields the client didn’t send are left as they were.
The arguments are easy to mix up. Call the method on db_hero, the row from the session, and pass hero_data, the dictionary. hero is the request model, so it isn’t the target.
6. Save the row and return what’s stored
Add the object to the session with session.add(), then call session.commit(). After you change the object in memory, add it to the session before you commit: in the endpoint, that’s session.add(db_hero) followed by session.commit(). The updated hero reaches the database only after the commit. After that, session.refresh() reloads the object, so the response reflects what’s actually stored.
Here’s the whole endpoint, written for SQLModel 0.0.14 or later. On an older version, swap model_dump for dict in the hero_data line and change nothing else.
@app.patch("/heroes/{hero_id}")
def update_hero(hero_id: int, hero: HeroUpdate, session: SessionDep):
db_hero = session.get(Hero, hero_id)
if not db_hero:
raise HTTPException(status_code=404, detail="Hero not found")
hero_data = hero.model_dump(exclude_unset=True)
db_hero.sqlmodel_update(hero_data)
session.add(db_hero)
session.commit()
session.refresh(db_hero)
return db_hero
Without FastAPI: plain SQLModel
Outside an endpoint, you follow the flow from the SQLModel tutorial. Select the row with results.one() and set the attribute on the object directly. Then save it the same way step 6 does.
statement = select(Hero).where(Hero.name == "Spider-Boy")
results = session.exec(statement)
hero = results.one()
hero.age = 16
session.add(hero)
session.commit()
session.refresh(hero)
The session.refresh(hero) right after the commit is there for the same reason as in the endpoint. session.commit() saves the updated hero in the database, and the refresh reloads the object so what you print or return matches what’s stored.
In the tutorial, Spider-Boy has age=None before this update. That holds only on a fresh database, so don’t expect it on one you’ve already written to.
The tutorial looks the hero up by name. Neither name nor id is required, but the tutorial notes that finding the row by id (WHERE id = 2) will probably be more common.
Sources
This article was created using generative AI based on publicly available sources.