简体   繁体   English

相当于Django 1.9中的sqlall?

[英]Equivalent of sqlall in Django 1.9?

I'm working in Django 1.9, and I'm trying to get the SQL that would be used to create database tables from models.py. 我正在使用Django 1.9,我正在尝试获取用于从models.py创建数据库表的SQL。

It seems that in previous version of Django one could do: 似乎在以前版本的Django中可以做到:

python manage.py sqlall <app_name>

But that no longer seems to be the case. 但似乎不再是这种情况。 I just get Unknown command: 'sqlall' . 我刚收到Unknown command: 'sqlall' Nothing in the docs seems to offer this. 文档中没有任何内容似乎提供此功能。

How can I see how my models are turned into SQL? 如何查看我的模型如何转换为SQL?

It is deprecated in Django 1.9 : 在Django 1.9中不推荐使用它:

The SQL management commands for apps without migrations, sql, sqlall, sqlclear, sqldropindexes, and sqlindexes, will be removed. 将删除没有迁移的应用程序的SQL管理命令,sql,sqlall,sqlclear,sqldropindexes和sqlindexes。

The closest command you can use is sqlmigrate : 您可以使用的最近的命令是sqlmigrate

Prints the SQL for the named migration. 打印指定迁移的SQL。 This requires an active database connection, which it will use to resolve constraint names; 这需要一个活动的数据库连接,它将用于解析约束名称; this means you must generate the SQL against a copy of the database you wish to later apply it on. 这意味着您必须针对您希望稍后应用它的数据库副本生成SQL。

You can use the migration name 0001_initial for the creation script, for example: 您可以使用迁移名称0001_initial作为创建脚本,例如:

python manage.py sqlmigrate myapp 0001_initial

I found that using sqlmigrate was a pretty rotten solution for me. 我发现使用sqlmigrate对我来说是一个非常糟糕的解决方案。 I'm setting up logical replications, and so I need the SQL of the database, not the SQL of individual migrations. 我正在设置逻辑复制,因此我需要数据库的SQL,而不是单个迁移的SQL。

The solution I found was to use pg_dump with the --schema-only option. 我找到的解决方案是使用pg_dump--schema-only选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM