运行前对表进行查询记录器.迁移(Django\u迁移)?

2024-06-25 07:12:25 发布

您现在位置:Python中文网/ 问答频道 /正文

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

修复_迁移.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 ....

谢谢。你知道吗


Tags: djangofrompyimportdbmigrationsconnectionrecord
1条回答
网友
1楼 · 发布于 2024-06-25 07:12:25

我找到了一个解决办法

首先:执行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")

工作。你知道吗

相关问题 更多 >