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

查看:293
本文介绍了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?

推荐答案

我需要在数据库上手动执行此操作吗?不支持任何简单的解决方案。

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 *选项到manage.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天全站免登陆