我正在Heroku上运行Django 5.1 Web应用程序,我希望保留用户数据。
在本地,我刚刚向现有模型添加了一个新的char
字段,并且在推送到Heroku时不想破坏任何内容。我知道Django 6引入了migrate
命令,但是Django 5并没有这样的命令。我只有South
迁移工具。
我尝试在本地跟踪South basic tutorial(在我的sqlite3数据库上),以确保在我运行“真正的'”时不会破坏任何内容。 Heroku。一切都破了......
(venv)$ python manage.py migrate forecasts
Running migrations for forecasts:
- Migrating forwards to 0002_auto__add_field_day_weather.
> forecasts:0001_initial
FATAL ERROR - The following SQL query failed: CREATE TABLE "forecasts_region" ("id" integer NOT NULL PRIMARY KEY, "url" varchar(200) NOT NULL UNIQUE, "name" varchar(200) NOT NULL, "nickname" varchar(10) NOT NULL)
The error was: table "forecasts_region" already exists
! Error found during real run of migration! Aborting.
! Since you have a database that does not support running
! schema-altering statements in transactions, we have had
! to leave it in an interim state between migrations.
! You *might* be able to recover with: = DROP TABLE "forecasts_region"; []
= DROP TABLE "forecasts_day"; []
= DROP TABLE "forecasts_tide"; []
! The South developers regret this has happened, and would
! like to gently persuade you to consider a slightly
! easier-to-deal-with DBMS (one that supports DDL transactions)
! NOTE: The error which caused the migration to fail is further up.
Error in migration: forecasts:0001_initial
DatabaseError: table "forecasts_region" already exists
我被迫DROP
这三张牌,然后重新运行python manage.py syncdb
,然后再python manage.py migrate forecasts
。这增加了新字段,但我丢失了这三个表中的所有数据。
我害怕在现场版本上弄乱了所有内容,所以请问,我该怎么做,按什么顺序?如果您可以包含最佳实践,以便在出现问题时保留数据,那将非常感激。另外,请亲自抓住我,因为我从未使用South
。谢谢!
答案 0 :(得分:4)
您正在使用Django South进行数据库迁移。上面运行的问题是您已经在数据库中创建了表。南让你做一个假的"您第一次运行迁移时迁移,因此它不会尝试创建已存在的表。
您可以尝试使用South命令将现有应用转换为South,如http://south.readthedocs.org/en/latest/convertinganapp.html#converting-an-app所述,或尝试以下操作:
在数据库中创建所有表。你在第一次启动项目时运行了这个
python manage.py syncdb
使用South
创建初始迁移python manage.py schemamigration --initial forecasts
将其应用为虚假迁移
python manage.py migrate forecasts --fake
更改预测模型。
为新的更改创建迁移
python manage.py schemamigration --auto forecasts
应用该迁移,现在只有一个alter命令
python manage.py migrate forecasts
答案 1 :(得分:0)
好的,我认为我在本地和Heroku上使用South找到了一个很好的分步过程(来自blog):
python django_project/manage.py syncdb
python django_project/manage.py convert_to_south django_app
python django_project/manage.py schemamigration django_app --auto
python django_project/manage.py migrate django_app
South==0.7.3
git push heroku master
heroku run bin/python django_project/manage.py syncdb
heroku run bin/python django_project/manage.py convert_to_south
django_app heroku run bin/python django_project/manage.py migrate django_app
以防万一,您可以使用PG Backups plugin按照here在heroku上备份postgres数据库。