SQLAlchemy ORM generally turns instance attributes that are None into NULL column values in the database (and vice-versa), but I'd like SQLAlchemy to convert between None values in the ORM and a specific value in the database (e.g. MyTable(latitude=None) is tied to mytable.latitude=-999.9 in the database).
Is there a way get SQLAlchemy to translate back and forth between None in ORM models and specific values in the database?
You can achieve this in SQLAlchemy with event listeners. In particular, with Mapper Events and Session Events.
I will use these event listeners:
before_insert,before_update,loaded_as_persistentto demonstrate how to do it. There might be other related events you will need to modify.Here's the example:
Sample output:
Table values from the database:
PS: You might want to just use getter/setter methods.