使用 South 在 Heroku 上设置 Django 时遇到问题 - 不断出现 ProgrammingError:关系不存在
Posted
技术标签:
【中文标题】使用 South 在 Heroku 上设置 Django 时遇到问题 - 不断出现 ProgrammingError:关系不存在【英文标题】:Trouble getting Django set up on Heroku using South - keep getting ProgrammingError: relation does not exist 【发布时间】:2014-04-09 18:29:16 【问题描述】:这就是我一直在做的:
本地 - 我有一个全新的 postgres 数据库,以及来自两个不同应用程序的两个 models.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
对我来说,看起来这些表甚至都没有被创建,但我不知道为什么没有。当我运行heroku run python manage.py sqlall
时,它说一切都已完成,但随后我查看了数据库本身(heroku 在 s3 上创建的那个),app_one 和 app_two 什么都没有。同样,这一切都在本地完美运行,只是当它在 heroku 上运行时,事情就崩溃了。
【问题讨论】:
如果你运行./manage.py migrate extapi
然后./manage.py migrate
会发生什么?
我在运行heroku run ./manage.py migrate extapi
后得到django.db.utils.ProgrammingError: relation "api_userprofile" does not exist
。目前在 extapi.models 的顶部有一个 from api.models import UserProfile
导入。将尝试改变进口......
【参考方案1】:
有一个循环导入应该通过推迟创建 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
【讨论】:
我也必须这样做;但奇怪的是我不得不多次运行syncdb,直到一切都解决了。然后我跑了假货。有点吓人:)【参考方案2】:删除您的迁移文件夹,然后执行
python manage.py makemigrations appname
python manage.py migrate --run-syncdb
python manage.py migrate --fake appname
【讨论】:
以上是关于使用 South 在 Heroku 上设置 Django 时遇到问题 - 不断出现 ProgrammingError:关系不存在的主要内容,如果未能解决你的问题,请参考以下文章
如何让 South 在 Heroku 中为 Django 应用程序工作
heroku 应用程序中新对象的时间戳设置为 heroku 的服务器启动时间,而不是实际的 utcnow 时间