在运行recorder.Migration之前对表进行查询(django_migrations)?

时间:2017-12-05 20:02:45

标签: python mysql django

我需要在表格 django_migrations 中写下我正在使用的内容:

fix_migration.py

from django.db import connection
from django.db.migrations import recorder
    recorder.MigrationRecorder(connection).record_applied("registro_movimientos", "0001_initial")

我通过python manage.py runscript fix_migration.py

申请

但我需要先检查以前是否应用了迁移,有些类似:

query = django_migrations.filter(name=0001_initial, app="registro_movimientos")

if not query:
    recorder.M ....
谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案

首先

:执行sql以搜索依赖项迁移

之后的

:执行sql以验证当前迁移是否已应用

from django.db import connection
from django.db.migrations import recorder

cursor = connection.cursor()
depends = cursor.execute(
    'SELECT name FROM django_migrations WHERE name = "0001_initial" AND app = "registro_movimientos"')

re = cursor.execute('SELECT name FROM django_migrations WHERE name = "0002_auto_20171205_1645" AND app = "registro_movimientos"')


if depends and not re:
    print("applying migration 0002_auto_20171205_1645 ... ")
    recorder.MigrationRecorder(connection).record_applied("registro_movimientos", "0002_auto_20171205_1645")

有效。