作者:手机用户2602936393 | 来源:互联网 | 2023-05-18 05:46
TryingtoseetheSQLthatsyncdbwouldgenerateatthecurrentmomentintime.试图查看syncdb将在当前时刻生成的
Trying to see the SQL that syncdb would generate at the current moment in time.
试图查看syncdb将在当前时刻生成的SQL。
After several searches the answer wasn't readily apparent -- I know you can use:
经过几次搜索,答案并不明显 - 我知道你可以使用:
python manage.py syncdb --sqlall
returns:
Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created.
How can I output the changes that have happened for the entire database if the code has changed at all?
如果代码发生了变化,如何输出整个数据库发生的更改?
Is there a way to generate all of the SQL for all of the apps that need syncdb'ing at this time? Or need I just explicitly state each app? I'm not looking for all of the SQL for the entire site, just for the changes that would be implemented by a syncdb.
有没有办法为所有需要syncdb'ing的应用程序生成所有SQL?或者我需要明确说明每个应用程序?我不是在寻找整个站点的所有SQL,只是针对syncdb实现的更改。
I've got several apps that need sql generated to describe the changes. I could explicitly list them, but is there a way for syncdb to figure this out for me ?
我有几个应用程序需要生成sql来描述更改。我可以明确地列出它们,但是syncdb有没有办法为我解决这个问题?
2 个解决方案
1
The Django Extensions package has a number of custom management commands for django, one of these commands is sqldiff:
Django Extensions包有许多用于django的自定义管理命令,其中一个命令是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
接下来,为已安装的应用添加django-extensions
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)
您将看到完整的差异列表,以及一长列ALTER TABLE语句,这些语句将确保正确设置所有字段(长度,空值,无符号等)
Any tables that are not created will be listed, and you can then dump the SQL to create them using
将列出任何未创建的表,然后您可以转储SQL以使用它们创建它们
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.
值得注意的是,对于列名更改,sqldiff将尝试简单地删除重命名的列并在其位置创建新列 - 因此,不要只复制整个sqldiff并在不检查的情况下针对生产运行它。