4

我有一个现有的 Django 项目,我刚刚将 South 添加到。

  • 我在本地运行了syncdb。
  • manage.py schemamigration app_name我在本地跑
  • manage.py migrate app_name --fake我在本地跑
  • 我提交并推送给heroku master
  • 我在heroku上运行了syncdb
  • manage.py schemamigration app_name在heroku上跑
  • manage.py migrate app_name在heroku上跑

然后我收到这个:

$ heroku run python notecard/manage.py migrate notecards
Running python notecard/manage.py migrate notecards attached to terminal... up, run.1
Running migrations for notecards:
 - Migrating forwards to 0005_initial.
 > notecards:0003_initial
Traceback (most recent call last):
  File "notecard/manage.py", line 14, in <module>
    execute_manager(settings)
  File "/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/app/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/app/lib/python2.7/site-packages/south/management/commands/migrate.py", line 105, in handle
    ignore_ghosts = ignore_ghosts,
  File "/app/lib/python2.7/site-packages/south/migration/__init__.py", line 191, in migrate_app
    success = migrator.migrate_many(target, workplan, database)
  File "/app/lib/python2.7/site-packages/south/migration/migrators.py", line 221, in migrate_many
    result = migrator.__class__.migrate_many(migrator, target, migrations, database)
  File "/app/lib/python2.7/site-packages/south/migration/migrators.py", line 292, in migrate_many
    result = self.migrate(migration, database)
  File "/app/lib/python2.7/site-packages/south/migration/migrators.py", line 125, in migrate
    result = self.run(migration)
  File "/app/lib/python2.7/site-packages/south/migration/migrators.py", line 99, in run
    return self.run_migration(migration)
  File "/app/lib/python2.7/site-packages/south/migration/migrators.py", line 81, in run_migration
    migration_function()
  File "/app/lib/python2.7/site-packages/south/migration/migrators.py", line 57, in <lambda>
    return (lambda: direction(orm))
  File "/app/notecard/notecards/migrations/0003_initial.py", line 15, in forwards
    ('user', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'])),
  File "/app/lib/python2.7/site-packages/south/db/generic.py", line 226, in create_table
    ', '.join([col for col in columns if col]),
  File "/app/lib/python2.7/site-packages/south/db/generic.py", line 150, in execute
    cursor.execute(sql, params)
  File "/app/lib/python2.7/site-packages/django/db/backends/util.py", line 34, in execute
    return self.cursor.execute(sql, params)
  File "/app/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
    return self.cursor.execute(query, args)
django.db.utils.DatabaseError: relation "notecards_semester" already exists

我有 3 个模型。部分、学期和记事卡。我在 Notecards 模型中添加了一个字段,但无法在 Heroku 上添加它。

谢谢你。

4

2 回答 2

5

您必须伪造创建表的迁移,然后照常运行其他迁移。

manage.py migrate app_name 000X --fake
manage.py migrate app_name 

000X 是您在其中创建表的迁移编号。

于 2012-04-13T05:48:22.427 回答
1

首先,从 0003_initial 和 0005_initial 的外观来看,您已经完成了多个schemamigration myapp --initial添加 create_table 语句的命令。拥有两组这些肯定会导致问题,因为一组将创建表,然后下一组将尝试创建现有表。

您的migrations文件夹可能完全被奇怪的迁移污染了。

无论如何,虽然我了解跑步的理论schemamigration本地机器和远程机器上运行的理论,但这可能是你问题的根源。Schemamigration 会生成一个新的迁移——如果你必须在你的开发服务器上运行它,提交它,推送它,然后在你的生产机器上再生成一个,你最终可能会得到重叠的迁移。

另一件事:如果您在远程计算机上运行 syncdb 并且它正在生成表,这意味着您的数据库是 100% 最新的——不需要迁移。你会做一个完整migrate --fake的匹配你的迁移到你的数据库。

I ran syncdb locally.
I ran manage.py schemamigration app_name locally
I ran manage.py migrate app_name --fake locally
I commit and pushed to heroku master 
I ran syncdb on heroku 

I ran manage.py schemamigration app_name on heroku
# if you ran syncdb, your DB would be in the final state.
I ran manage.py migrate app_name on heroku
# if you ran syncdb, your DB would be in the final state. Nothing to migrate.
于 2012-04-13T06:28:11.357 回答