StaleDataError on simple update statement

55 views
Skip to first unread message

Mirel Glejser

unread,
Apr 26, 2023, 6:23:55 AM4/26/23
to sqlalchemy
I am completely stumped after working on this issue for 2 days.

I am simply querying a user, updating the name, and sending it back to the database.

user = session.query(User).get(1)
user.first_name # John 
user.first_name = 'Sally' 
session.commit()

# > sqlalchemy.orm.exc.StaleDataError: UPDATE statement on table 'user' expected to update 1 row(s); # -1 were matched.

What is causing this error?

Mike Bayer

unread,
Apr 26, 2023, 11:07:03 AM4/26/23
to noreply-spamdigest via sqlalchemy
this can happen if you are using the wrong datatype for your primary key.  like it's a string in the database and you are referring to it as an Integer, that kind of thing.

no way to provide more detail without seeing a short version of the mapping that produces this error and CREATE TABLE statement used in the database.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages