flask-migrate 按字母顺序进行迁移
flask-migrate make migrations in alphabetical order
使用 flask-migrate
和 flask-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
使用 flask-migrate
和 flask-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