在Django中更改数据库表 [英] Altering database tables in Django

查看:108
本文介绍了在Django中更改数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用Django作为我开始的项目(fyi,一个基于浏览器的游戏),我最喜欢的功能之一是使用 syncdb 基于我定义的Django模型自动创建数据库表(在任何其他框架中我似乎找不到的功能)。
我已经认为这太好了,当我在文档

I'm considering using Django for a project I'm starting (fyi, a browser-based game) and one of the features I'm liking the most is using syncdb to automatically create the database tables based on the Django models I define (a feature that I can't seem to find in any other framework). I was already thinking this was too good to be true when I saw this in the documentation:


Syncdb不会更改现有表格

Syncdb will not alter existing tables

syncdb只会为尚未安装的模型创建表。它永远不会发出ALTER TABLE语句来匹配安装后对模型类所做的更改。对模型类和数据库模式的更改通常涉及某种形式的歧义,在这些情况下,Django必须猜测正确的更改。有一个风险,关键数据将在过程中丢失。

syncdb will only create tables for models which have not yet been installed. It will never issue ALTER TABLE statements to match changes made to a model class after installation. Changes to model classes and database schemas often involve some form of ambiguity and, in those cases, Django would have to guess at the correct changes to make. There is a risk that critical data would be lost in the process.

如果您对模型进行了更改,并希望更改数据库表匹配,请使用sql命令显示新的SQL结构并将其与现有的表模式进行比较,以计算出更改。

If you have made changes to a model and wish to alter the database tables to match, use the sql command to display the new SQL structure and compare that to your existing table schema to work out the changes.

将必须手动完成。

我想知道的是最好的方法。有两种解决方案:

What I would like to know is the best way to do this. Two solutions come to mind:


  • 根据文档建议,在DB中手动更改;

  • 执行数据库的备份,擦除它,再次创建表(使用syncdb,因为现在它正在从头创建表)并导入备份的数据(如果数据库很大,这可能需要很长时间) li>
  • As the documentation suggests, make the changes manually in the DB;
  • Do a backup of the database, wipe it, create the tables again (with syncdb, since now it's creating the tables from scratch) and import the backed-up data (this might take too long if the database is big)

任何想法?

推荐答案

请注意在同一主题的其他答案中,请务必观看 DjangoCon 2008 Schema Evolution Panel

As noted in other answers to the same topic, be sure to watch the DjangoCon 2008 Schema Evolution Panel on YouTube.

此外,地图上的两个新项目: Simplemigrations 移徙

Also, two new projects on the map: Simplemigrations and Migratory.

这篇关于在Django中更改数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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