5

试图查看 syncdb 将在当前时刻生成的 SQL。

经过几次搜索,答案并不明显——我知道你可以使用:

python manage.py syncdb --sqlall

返回:

Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created.

如果代码发生了变化,如何输出整个数据库发生的变化?

有没有办法为此时需要同步数据库的所有应用程序生成所有 SQL?还是需要我明确说明每个应用程序?我不是在寻找整个站点的所有 SQL,而只是寻找将由 syncdb 实现的更改。

我有几个需要生成 sql 来描述更改的应用程序。我可以明确列出它们,但是 syncdb 有没有办法为我解决这个问题?

4

2 回答 2

6

你可以做

./manage.py sqlall <app_name>

获取应用程序的 sql 语句和初始数据。

如果你只想要sql 语句

./manage.py sql <app_name>

这是一个打印所有已安装应用程序的 sqlall的管理命令。或者,您可以编写自己的管理命令来获取所有已安装的应用程序,并./manage.py sql <app_name>为每个应用程序调用。

于 2013-06-10T00:04:12.183 回答
1

The Django Extensions package has a number of custom management commands for django, one of these commands is sqldiff:

https://github.com/django-extensions/django-extensions/blob/master/docs/sqldiff.rst

First,

sudo pip install django-extensions

Next, add django-extensions to installed apps

INSTALLED_APPS = (
    ...
    'django_extensions',
    ...
)

Then, you can

python manage.py sqldiff -a

You'll be presented with a full list of differences, as well as a long list of ALTER TABLE statements that will ensure all fields are set properly (length, null, unsigned, etc)

Any tables that are not created will be listed, and you can then dump the SQL to create them using

python manage.py sqlall {app_label}

It's worth noting that for column name changes, sqldiff will attempt to simply drop renamed columns and create new ones in their place - So don't just copy the entire sqldiff and run it against production without inspecting.

于 2014-04-07T00:30:46.840 回答