Django-South-是否有办法查看它所运行的SQL?[英] Django - South - Is There a way to view the SQL it runs?

本文是小编为大家收集整理的关于Django-South-是否有办法查看它所运行的SQL?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这是我想做的.

使用开发数据库开发开发服务器上的Django项目.当我更改模型时,请根据需要运行南方迁移.

从每个迁移中保存SQL,并在我准备部署时将其应用于生产服务器.

南方可能会有这样的事情吗? (我也很好奇别人在使用Django时会做些什么来使您的开发数据库更改)

)

推荐答案

您至少可以检查执行manage.py migrate --db-dry-run --verbosity=2生成的SQL.这不会对数据库做任何事情,并将显示所有SQL.不过,我仍然会备份,比后悔更好.

其他推荐答案

 python manage.py sqlmigrate <app_label> <migration_name>

其他推荐答案

您可以尝试在db.connection.queries中记录sql查询,使用管理命令,该命令以干式运行选项调用迁移:

from django.core.management.base import BaseCommand
from django import db

class Command(BaseCommand):
    help = 'Output SQL for migration'

    def handle(self, *app_labels, **options):
        # assumes DEBUG is True in settings
        db.reset_queries()

        from django.core.management import call_command
        kw = {'db-dry-run': 1,  'verbosity': 0}
        call_command('migrate', **kw)

        for query in db.connection.queries:
            print query['sql']

假设南方将所有内容都通过通常可行的DB接口放置.当查询历史记录表时,其中会有一些额外的选择.

您将其放入应用程序内,然后运行:

python manage.py print_migration_sql

它可能很容易扩展以仅针对特定应用等运行

本文地址:https://www.itbaoku.cn/post/597577.html

问题描述

Here's what I want to do.

Develop a Django project on a development server with a development database. Run the south migrations as necessary when I change the model.

Save the SQL from each migration, and apply those to the production server when I'm ready to deploy.

Is such a thing possible with South? (I'd also be curious what others do to get your development database changes on production when working with Django)

推荐答案

You can at least inspect the sql generated by doing manage.py migrate --db-dry-run --verbosity=2. This will not do anything to the database and will show all the sql. I would still make a backup though, better safe than sorry.

其他推荐答案

 python manage.py sqlmigrate <app_label> <migration_name>

其他推荐答案

You could try logging the SQL queries in db.connection.queries, using a management command that calls the migrate with a dry-run option:

from django.core.management.base import BaseCommand
from django import db

class Command(BaseCommand):
    help = 'Output SQL for migration'

    def handle(self, *app_labels, **options):
        # assumes DEBUG is True in settings
        db.reset_queries()

        from django.core.management import call_command
        kw = {'db-dry-run': 1,  'verbosity': 0}
        call_command('migrate', **kw)

        for query in db.connection.queries:
            print query['sql']

Assuming that south puts everything through the usual db interface that should work. There will be a few extra selects in there when it queries the history table.

You'd put that in a management/commands/print_migration_sql.py inside your app and then run it:

python manage.py print_migration_sql

It could probably be easily extended to run this only for specific apps etc