Flask-Migrate
Flask-Migrate copied to clipboard
Generating infinite migration
Hi, how are you? I have a problem I'm using flask migrate, but it's generating migrations without any changes in the models, these are the logs when the migration is generated
INFO [alembic.autogenerate.compare] Detected type change from REAL() to FLOAT(precision=3)
INFO [alembic.autogenerate.compare] Detected type change from REAL() to FLOAT(precision=3)
Generating .py file is this:
"""Teste
Revision ID: a63d674f101a
Revises: 290d1d6205d8
Create Date: 2021-10-04 15:07:25.072999
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'a63d674f101a'
down_revision = '290d1d6205d8'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('product', 'value',
existing_type=sa.REAL(),
type_=sa.Float(precision=2),
existing_nullable=True)
op.alter_column('evaluation', 'average',
existing_type=sa.REAL(),
type_=sa.Float(precision=3),
existing_nullable=False)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('product', 'average',
existing_type=sa.Float(precision=3),
type_=sa.REAL(),
existing_nullable=False)
op.alter_column('evaluation', 'value',
existing_type=sa.Float(precision=2),
type_=sa.REAL(),
existing_nullable=True)
# ### end Alembic commands ###
At first I thought this issue was because I was on flask-migrate version 2.5.2 but I upgraded to the latest version this issue remains. If I run flask db migrate -m "foo" again it shows the message "No changes in schema detected", however when I run flask db migrate -m "foo" after running flask db upgrade it generates the same migration , always with the same modifications
Thanks. May you have a great day or afternoon or night haha
This might be a bug in alembic. Flask-Migrate does not generate migrations, it is just a wrapper for alembic.
Thank you very much, I updated alembic and generate the files again, the only difference was in the env.py file The difference was in the alembic version 1.4.2 it was like this
from flask import current_app
config.set_main_option(
'sqlalchemy.url', current_app.config.get(
'SQLALCHEMY_DATABASE_URI').replace('%', '%%'))
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True,
compare_type=True
)
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True,
compare_type=True
)
and in version 1.4.3 the env.py file looks like this
from flask import current_app
config.set_main_option(
'sqlalchemy.url',
str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%'))
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True
)
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
process_revision_directives=process_revision_directives,
**current_app.extensions['migrate'].configure_args
)
You are confusing the role of Flask-Migrate and Alembic. The env.py file is provided by Flask-Migrate, and it just configures everything for Alembic. I don't understand if the problem continues or not, but as I said before, if you think no migration should have been generated, then the issue is with Alembic, which is were migrations are generated.
I am having the same issue with alembic in fastapi with postgresql. So, issue is definitely not related with Flask-migrate.
@GustavoSwDaniel Did you find a solution?
I fixed this by changing compare_type in the context.configure. This will prohibit you from changing types in your SqlAlchemy models using Alembic however.
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True,
compare_type=False
)
@libdib-nalexander I tried this but didn't fix it up for me. @GustavoSwDaniel did you find any working solution for this ? The newer alembic versions are fixing previous issues and giving me new ones.
I am running into this same issue
I'm going to reiterate that Flask-Migrate does not generate migrations. You are using Alembic for your migrations. Flask-Migrate just makes Alembic easier to use with Flask. I'm closing, since there is really nothing I can do from the Flask-Migrate side to help with this.