[英]Dump database and use it later after deleting migrations in django 1.11
Sometimes I have to delete migrations for some reason and I will have to create new data again for django. 有时由于某种原因我不得不删除迁移,而我将不得不再次为django创建新数据。 How do I dump the db
and use it later after I delete the migrations? 删除迁移后,如何转储db
并在以后使用它?
You have to use the dumpdata
command. 您必须使用dumpdata
命令。
The following command will dump the whole database to a db.json
file. 以下命令会将整个数据库转储到db.json
文件中。
./manage.py dumpdata > db.json
After that, you can restore the data into db with loaddata
command. 之后,您可以使用loaddata
命令将数据还原到db中。
But, if you have made any critical change you might get the IntegrityError
. 但是,如果进行了任何重要更改,则可能会收到IntegrityError
。 To fix this issue you have to make sure to backup the database by excluding contenttypes
and auth.permissions
tables by running this command when dumping data: 要解决此问题,您必须确保在转储数据时通过运行以下命令来排除内容contenttypes
和auth.permissions
表来备份数据库:
./manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json
After that you can safely run loaddata
command with a fresh database. 之后,您可以使用新数据库安全地运行loaddata
命令。
./manage.py loaddata db.json
Here you can find Django documentation on dumpdata command . 在这里,您可以找到关于dumpdata命令的 Django文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.