2014-03-06 28 views
14

这是我一直在做的事情:麻烦的Django建立在Heroku利用南 - 不断收到ProgrammingError:关系不存在

本地 - 在那里我有一个全新的Postgres数据库,以及两种机型.py文件从两个不同的应用程序:

python manage.py syncdb 
python manage.py schemamigration api --initial 
python manage.py schemamigration extapi --initial 
python manage.py migrate api 0001 --fake 
python manage.py migrate extapi 0001 --fake 

这工作膨胀,我可以添加东西到数据库就好了。

然后,推到Heroku的,在那里我已经创建了一个空的应用程序时:

git add . 
git commit -m "Ready to go to Heroku" 
git push heroku master 
heroku run python manage.py syncdb 

输出这样的:

Running `python manage.py syncdb` attached to terminal... up, run.9548 
Syncing... 
Creating tables ... 
Creating table django_admin_log 
Creating table auth_permission 
Creating table auth_group_permissions 
Creating table auth_group 
Creating table auth_user_groups 
Creating table auth_user_user_permissions 
Creating table auth_user 
Creating table django_content_type 
Creating table django_session 
Creating table south_migrationhistory 

# create superuser prompt... 
Superuser created successfully. 
Installing custom SQL ... 
Installing indexes ... 
Installed 0 object(s) from 0 fixture(s) 

Synced: 
> django.contrib.admin 
> django.contrib.auth 
> django.contrib.contenttypes 
> django.contrib.sessions 
> django.contrib.messages 
> django.contrib.staticfiles 
> south 
> rest_framework 

Not synced (use migrations): 
- api 
- extapi 

然后我试着将这些应用程序迁移与heroku run python manage.py migrate并获得这个错误:

Running `python manage.py migrate` attached to terminal... up, run.3724 
Running migrations for api: 
- Migrating forwards to 0001_initial. 
> api:0001_initial 
FATAL ERROR - The following SQL query failed: ALTER TABLE "api_song" ADD CONSTRAINT "summary_id_refs_id_36bb6e06" FOREIGN KEY ("summary_id") REFERENCES "extapi_summary" ("id") DEFERRABLE INITIALLY DEFERRED; 
The error was: relation "extapi_summary" does not exist 

Error in migration: api:0001_initial 
Traceback (most recent call last): 
    File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line 
    utility.execute() 
    File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv 
    self.execute(*args, **options.__dict__) 
    File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute 
    output = self.handle(*args, **options) 
    File "/app/.heroku/python/lib/python2.7/site-packages/south/management/commands/migrate.py", line 111, in handle 
    ignore_ghosts = ignore_ghosts, 
    File "/app/.heroku/python/lib/python2.7/site-packages/south/migration/__init__.py", line 220, in migrate_app 
    success = migrator.migrate_many(target, workplan, database) 
    File "/app/.heroku/python/lib/python2.7/site-packages/south/migration/migrators.py", line 254, in migrate_many 
    result = migrator.__class__.migrate_many(migrator, target, migrations, database) 
    File "/app/.heroku/python/lib/python2.7/site-packages/south/migration/migrators.py", line 329, in migrate_many 
    result = self.migrate(migration, database) 
    File "/app/.heroku/python/lib/python2.7/site-packages/south/migration/migrators.py", line 133, in migrate 
    result = self.run(migration, database) 
    File "/app/.heroku/python/lib/python2.7/site-packages/south/migration/migrators.py", line 114, in run 
    return self.run_migration(migration, database) 
    File "/app/.heroku/python/lib/python2.7/site-packages/south/migration/migrators.py", line 85, in run_migration 
    south.db.db.execute_deferred_sql() 
    File "/app/.heroku/python/lib/python2.7/site-packages/south/db/generic.py", line 318, in execute_deferred_sql 
    self.execute(sql) 
    File "/app/.heroku/python/lib/python2.7/site-packages/south/db/generic.py", line 282, in execute 
    cursor.execute(sql, params) 
    File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute 
    return self.cursor.execute(sql, params) 
    File "/app/.heroku/python/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute 
    return self.cursor.execute(sql, params) 
django.db.utils.ProgrammingError: relation "extapi_summary" does not exist 

对我来说,它看起来像表只是不是cr吃了,但我不知道为什么不。当我运行heroku run python manage.py sqlall时,它表示一切都已经完成,但后来我查看了数据库本身(heroku在s3上制作的一个),app_one和app_two中没有任何内容。再次,这一切都在当地完美实现,只是当它在heroku上出现问题时才会崩溃。

+0

如果运行'./manage.py migrate extapi'然后'./manage.py migrate',会发生什么? –

+0

运行'heroku run ./manage.py migrate extapi'后,得到'django.db.utils.ProgrammingError:relation“api_userprofile”不存在“。目前在extapi.models的顶部有一个来自api.models import UserProfile的导入。要尝试改变进口... –

+0

是的,同样的问题。 –

回答

21

有一个循环导入应该通过推迟创建api_userprofile来处理,但是由于South处理事务的方式,它会中断。

所以!使这项工作最简单的方法是让执行syncdb让所有的表,只是假冒迁移:

python manage.py syncdb --all 

这就使我们:

Synced: 
> django.contrib.admin 
> django.contrib.auth 
> django.contrib.contenttypes 
> django.contrib.sessions 
> django.contrib.messages 
> django.contrib.staticfiles 
> api 
> extapi 
> moodranker 
> recommender 
> south 
> rest_framework 

Not synced (use migrations): 
- 

然后假冒迁移:

python manage.py migrate --fake 
+0

我不得不这样做;但奇怪的是我不得不多次运行syncdb,直到解决了所有问题。然后我跑了假货。有点可怕:) –

+0

大把戏。谢谢你一吨安德鲁:) – chhantyal

2

删除你的migrations文件夹,然后做

python manage.py makemigrations appname 

python manage.py migrate --run-syncdb 

python manage.py migrate --fake appname