Django模型同步表 [英] Django Model Sync Table

查看:173
本文介绍了Django模型同步表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我更改了Django模型中的一个字段,那么我如何与数据库表进行同步呢?我需要在数据库上手动执行,还是有一个帮助过程的工具?

If I change a field in a Django model, how can I synchronize it with the database tables? Do I need to do it manually on the database or is there a tool that does helps with the process?

推荐答案

唉,Django不支持任何简单的解决方案。

Alas, Django does not support any easy solution to this.

django将为您做的唯一的事情是使用与新模型匹配的新表重新启动数据库:

The only thing django will do for you, is restart your database with new tables that match your new models:

$ #DON'T DO THIS UNLESS YOU CAN AFFORD TO LOSE ALL YOUR DATA!
$ python PROJECT_DIR/manage.py syncdb

下一个选项是使用各种sql * options.py的选项,以查看django将如何将当前模型与数据库匹配,然后发出自己的 ALTER TABLE 命令,使一切正常。当然这是容易出错的难题。

the next option is to use the various sql* options to manage.py to see what django would do to match the current models to the database, then issue your own ALTER TABLE commands to make everything work right. Of course this is error prone and difficult.

真正的解决方案是使用数据库迁移工具,例如 south 生成迁移代码。

The real solution is to use a database migration tool, such as south to generate migration code.

这是一个相似的问题,讨论django的各种数据库迁移选项。

Here is a similar question with discussion about various database migration options for django.

这篇关于Django模型同步表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆