Migrations - Flask-Migrate (4)

先在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

检查数据库:
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值