热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Django在代码更改后获取syncdbsqlall语句进行更新-Djangogetsyncdbsqlallstatementtoupdateaftercodechanges

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


6  

You can do

你可以做

./manage.py sqlall 

to get the sql statements and initial data for the app.

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

If you want just the sql statements,

如果你只想要sql语句,

./manage.py sql 

Here is a mangement command that prints sqlall for ALL installed apps. Alternatively, you can write your own management command which gets all the installed apps, and calls the ./manage.py sql for each.

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

#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并在不检查的情况下针对生产运行它。


推荐阅读
author-avatar
手机用户2602936393
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有