1. Michael Bayer
  2. alembic
  3. Issues


Issue #84 new

add some kind of option/recipe to turn TinyInt + length 1 into a boolean for mysql

Lucian Ursu
created an issue

I have a field which is defined as Boolean in my SQLAlchemy models and represented as TinyINT in the MySQL database. Every time I run alembic revision --autogenerate, the migration contains code to convert the field from TinyINT to Boolean.

Comments (2)

  1. Michael Bayer repo owner

    OK so this implies you have compare_type, which is False by default, set to True. A change in column type is rare so normally I'd just leave this off.

    If you do really want to have type comparison on, then you have to be ready for mismatches like this - MySQL doesn't have a boolean type, it has TINYINT. Some years back, we decided that while Boolean can generate a TINYINT, we shouldn't be reflecting TINYINT as Boolean, since that's not what it really is. So here you are seeing that. You can of course change the rules yourself using the compare_type callable documented at https://alembic.readthedocs.org/en/latest/api.html#alembic.environment.EnvironmentContext.configure:

    def my_compare_type(context, inspected_column,
            metadata_column, inspected_type, metadata_type):
        if isinstance(inspected_type, mysql.TINYINT) and isinstance(metadata_type, Boolean):
            return False
            return None

    The MySQL context within Alembic does have a space it could jump in here and look for TINYINT + length one, so maybe we can add a more complete version of this rule into the impl, but stick with either keeping type comparison off, or using a custom rule, for now.

  2. Log in to comment