Search code examples
pythonmysqlsqlalchemyalembic

Creating Double precision columns using SqlAlchemy from Alembic


I need to use a DOUBLE column in my MYSQL db. I've read the docs, which suggest I use a Float with precision of 64. This, however, doesn't seem to be working. I end up with a regular float column, and the required precision does not exist.

I also tried:

from sqlalchemy.dialects.mysql import DOUBLE  #and in the model:
item = db.Column(DOUBLE())

However, when migrating, Flask-Migrate doesn't seem to be able to tell a difference between the previous column and the new one, and generates an empty migration.

I read this: No changes detected in Alembic autogeneration of migrations with Flask-SQLAlchemy and https://github.com/miguelgrinberg/Flask-Migrate/issues/24

And tried setting compare_type=True in the alembic settings, but there is still no difference registered between Float and Double types.

I know I can just manually switch the columns in the db, but how can I enforce a double precision column using sqlalchemy?


Solution

  • Setting compare_type=True on the EnvironmentContext should work (and does work for me). If you are still having issues trying to auto-generate this, you can always just add this operation to the migration manually.

    def upgrade():
        #...
        op.alter_column('my_table', 'my_column', type_=DOUBLE, existing_nullable=False)
        #...
    

    As a general note, Alembic is not perfect at auto-generating migrations. It's a good idea to check the migrations scripts and edit them as needed first.