Short answer (using sqlalchemy version 1.4.25
):
From the documentation:
For user-defined types, that is, any custom type that is not within
the sqlalchemy. module namespace, by default Alembic will use the
value of __module__ for the custom type:
Column("my_column", myapp.models.utils.types.MyCustomType())
The imports for the above type again must be made present within
the migration, either manually, or by adding it to script.py.mako.
So, import your my_module
in script.py.mako
(and you probably need to define your custom type in a file other than models.py
):
...
from alembic import op
import sqlalchemy as sa
import my_module
${imports if imports else ""}
...
Long answer:
I have my custom type BinaryUUID
defined in uuid_type_mysql.py
and I'm importing it in models.py
and using it there:
models.py
:
...
from .uuid_type_mysql import BinaryUUID
...
After generating the migrations with flask db migrate
I would get this on the migration file:
...
sa.Column('public_id', my_module.uuid_type_mysql.BinaryUUID(length=16), nullable=False),
...
And the problem is that the migration does not know my_module
because it is not imported.
After adding import my_module
to script.py.mako
as suggested in the documentation, now the module is imported in the migration file:
...
from alembic import op
import sqlalchemy as sa
import my_module
...
After this, everything work fine for me with the bonus that it is not needed to manually edit the generated migration.