We recently changed a small part of the schema of the database for our project. Originally, one of the columns allowed NULL as it was never utilized. We recently implemented a feature that utilized this column, and now require it to be loaded with an integer. We have done this by adding the following to the table definition:
Column("amount", Integer, nullable = False, default = 1)
Obviously, this is only run if we are creating a new table. Most of our users have a pre-existing database, so we also run a migration that converts all previous values to 1. This works greats.
However, if we have a user downgrade for any reason and do something with the program that adds a row, the value will again be NULL. When they upgrade again, the conversion will not be run again. This is a limitation in our (simple) migration process (it increments DB version, but does not decrement, so it will not know to run the conversion as the database was already incremented)
This causes an issue because now we have a NULL value that is not expected. Is there any way to define a default value in SQLAlchemy when it is loaded from the database, and to set one if the value is illegal?