gpt4 book ai didi

python - flask-migrate 按字母顺序进行迁移

转载 作者:太空宇宙 更新时间:2023-11-04 11:19:32 25 4
gpt4 key购买 nike

使用 flask-migrateflask-script,我设置了我的项目,这样我只需要做

python manage.py db migrate

在迁移文件夹中,我得到如下文件

0f46602752b7_.py
8fdf8259859b_.py

无法保证第一次迁移先于第二次迁移。 Django 通过在所有迁移前加上一个自动递增的数字来解决这个问题。我们可以告诉 flask-migrate/alembic 做同样的事情吗?

理想情况下,上面示例中的两个文件是

001_8fdf8259859b_.py
002_0f46602752b7_.py

最佳答案

如果您检查每个迁移文件,您会发现如下行:

revision = '09364330399c'
down_revision = None

down_revision 代表之前的迁移。如果你真的想改变命名约定,你可以通过将 file_template 字段添加到你的 alembic.ini

来实现

以下文档:

file_template - this is the naming scheme used to generate new migration > files. The value present is the default, so is commented out. Tokens available include:

   %%(rev)s - revision id

%%(slug)s - a truncated string derived from the revision message

%%(year)d, %%(month).2d, %%(day).2d, %%(hour).2d, %%(minute).2d, %%(second).2d - components of the create date, by default datetime.datetime.now() unless the timezone configuration option is also used.

对于您的特定示例,在 alembic.ini

中添加以下行
file_template = %%(year)d%%(month).2d%%(day).2d_%%(hour).2d%%(minute).2d%%(second).2d_%%(rev)s_%%(slug)s

它将生成一个文件名,例如

20190527_122029_de2c595ec169_hello_world.py

关于python - flask-migrate 按字母顺序进行迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56306156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com