先在project目录下输入
$ flask db downgrade
$ psql database_name
INSERT INTO table_name (description) VALUES ('this is a todo');
INSERT INTO table_name (description) VALUES ('this is a todo1');
INSERT INTO table_name (description) VALUES ('this is a todo2');
在versions文件夹下找出哪一个是revision python file. 将代码稍微修改一下
"""empty message
Revision ID:
Revises:
Create Date:
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'f'
down_revision = 'f'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
# Make the nullable to be True
op.add_column('todos', sa.Column('completed', sa.Boolean(), nullable=True))
op.execute('UPDATE todos SET completed=False WHERE completed IS NULL')
op.alter_column('todos', 'completed', nullable=False)
# ### end Alembic commands ###
# It drops that completed column and it will also drop it on all existing data as well.
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('todos', 'completed')
# ### end Alembic commands ###
改完代码后,运行下面的命令
$ flask db upgrade
检查数据库: