flask-sqlalchemy
flask-sqlalchemy copied to clipboard
Misleading __repr__
The standard repr is misleading such that:
instance_state = inspect(item)
print(f"{item = }; {instance_state.transient = }; {instance_state.pending = }")
# item = <Competition (transient 140147915565184)>; instance_state.transient = False; instance_state.pending = True
I spent a small amount of time debugging why my instance was transient after I thought it should already be in the session and therefore pending. SQLAlchemy defines transient as:
an instance that’s not in a session, and is not saved to the database; i.e. it has no database identity. The only relationship such an object has to the ORM is that its class has a Mapper associated with it.
On the surface, the repr looks as though it is presenting the instance states as they are defined by SQLAlchemy. Given that the word "transient" has a very specific meaning, overloading it here with a different meaning doesn't quite feel right to me.
Can we afford to be more specific about object state in the repr or use an alternate word to "transient"? Or has the ship sailed?
https://github.com/pallets/flask-sqlalchemy/blob/9a467c19a6b58bdbbff55eb16dcfb6337a51ace6/src/flask_sqlalchemy/model.py#L132-L140