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 ###
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:
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.
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.