Home  >  Q&A  >  body text

flask - python manage.py db upgrade 语句不起作用

最近在学Flask,书籍是《Flask.Web.Development》,其中数据库迁移工具使用的是Flask-Migrate,书中提到创建迁移脚本后更新数据库使用python manage.py db upgrade就可以更新数据库,但是我在使用过程中模型修改后使用上述命令并不起作用,每次都需要删除所有表然后重建,不知道使用过程中在哪一步出错了?

可以参考这个文章,Flask Web Development —— 数据库(下)
就是翻译我提到的书籍;第一步和第二步都已经做了,按照书中所说,当模型更改的时候只要执行更新语句,数据库对表进行更新而不会删除表中的数据,但是现在不起作用,比如我模型增加了字段,执行语句后数据库表对应字段没有增加。

ringa_leeringa_lee2743 days ago956

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 14:00:26

    Database migration is generally divided into two steps

    • Generate migration script
    • Run the script and change the database

    Before upgrade you need migrate

    After modifying model
    Need to python manage.py db migrate
    first Then python manage.py db upgrade

    reply
    0
  • 阿神

    阿神2017-04-17 14:00:26

    The problem I encountered was after changing the existing field type migration. There are no changes in the migration script. Solve. . . .

    # revision identifiers, used by Alembic.
    revision = '5588e49798f0'
    down_revision = '53ffda24441'
    
    from alembic import op
    import sqlalchemy as sa
    
    
    def upgrade():
        ### commands auto generated by Alembic - please adjust! ###
        pass
        ### end Alembic commands ###
    
    
    def downgrade():
        ### commands auto generated by Alembic - please adjust! ###
        pass
        ### end Alembic commands ###

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:00:26

    The title’s question seems to have gone beyond the scope of the original book.

    The first time you enter python hello.py db upgrade is equivalent to db.creat_all(), and the next time you enter python hello.py db upgrade is to update the database. The questioner has already taken the first step, and now his attempt to update fails.

    So the problem should occur in the step of updating the database. Post how you subsequently modified the database to continue analyzing the problem.
    I am also currently studying, so I don’t know if this understanding is correct.

    reply
    0
  • Cancelreply