Flask-migrate: autogenerated upgrade and downgrade seems to be wrong.

Created on 7 Nov 2018  路  3Comments  路  Source: miguelgrinberg/Flask-Migrate

I have two tables A and B and after running the initial migration the autogenerated code is as follows

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('a')
    op.drop_table('b')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('a',
    sa.Column('id', sa.INTEGER(), nullable=False),
    sa.Column('date', sa.DATE(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('b',
    sa.Column('id', sa.INTEGER(), nullable=False),
    sa.Column('user_id', sa.VARCHAR(length=100), nullable=True),
    sa.Column('password', sa.VARCHAR(length=64), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )

I believe the expected generated code should be

def upgrade()
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('a',
    sa.Column('id', sa.INTEGER(), nullable=False),
    sa.Column('date', sa.DATE(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('b',
    sa.Column('id', sa.INTEGER(), nullable=False),
    sa.Column('user_id', sa.VARCHAR(length=100), nullable=True),
    sa.Column('password', sa.VARCHAR(length=64), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('a')
    op.drop_table('b')
    # ### end Alembic commands ###
question

Most helpful comment

This extension is designed to work with Flask-SQLAlchemy, not with standalone SQLAlchemy. The declarative_base that you are using is from SQLAlchemy. With Flask-SQLAlchemy your base class should be db.Model.

All 3 comments

It seems the A and B tables are in your database, and there are no models in your code that match those tables. So Alembic decides the tables need to be deleted.

You have two issues:

  • If you are adding new tables, you need to make sure those tables do not exist in your database when you generate the migration. The tables only need to be defined in your code as models.
  • The models for the added tables need to be visible to Alembic. If you have those models defined, then it is likely that you are not importing them, so they don't exist in the runtime context of your application. Try importing those models, even if they do not need to be used.

I had declared the model as
```
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

class A(Base):
pass


I changed it to 

class A(db.model):
pass
```
The autogenerated code looks to be correct now. I'm just starting out and don't know the difference between declarative_base() and db.model.
Thanks for the help.

This extension is designed to work with Flask-SQLAlchemy, not with standalone SQLAlchemy. The declarative_base that you are using is from SQLAlchemy. With Flask-SQLAlchemy your base class should be db.Model.

Was this page helpful?
0 / 5 - 0 ratings