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将只为尚未安装的模型创建表。它永远不会发出ALTER TABLE语句来匹配安装后对模型类所做的更改。对模型类和数据库模式的更改通常涉及某种形式的模糊性,在这种情况下,Django必须猜测要做出的正确更改。在此过程中存在关键数据丢失的风险。
如果您已经对模型进行了更改,并希望更改数据库表以匹配,请使用sql命令显示新的sql结构,并将其与现有的表模式进行比较,以确定更改。
看来,修改现有的表格将不得不“手工”完成。
我想知道的是最好的方法。想到了两种解决方案:
- 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)
有什么想法吗?