无法在 heroku 上进行 RDS 查询
unable to make RDS queries on heroku
我可以在本地机器上访问我的 RDS postresql 数据库没有问题。
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'xxxxxxx',
'USER': 'XXXXXXXX',
'PASSWORD': 'XXXXXXXX',
'HOST': 'XXXXXXrds.amazonaws.com',
'PORT': '5432',
}
我将其推送到 Heroku,然后在 /saferdb/query/
处收到 ProgrammingError
在 heroku 的 manage.py shell 中,我尝试访问数据库:
>>> from saferdb.models import Question
>>> q = Question.objects.all()
>>> q.count()
出现以下错误:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "saferdb_question" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "saferdb_question"
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 387, in count
return self.query.get_count(using=self.db)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/query.py", line 491, in get_count
number = obj.get_aggregation(using, ['__count'])['__count']
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/query.py", line 476, in get_aggregation
result = compiler.execute_sql(SINGLE)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1063, in execute_sql
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "saferdb_question" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "saferdb_question"
^
错误是因为 table 不存在于数据库中,这意味着您需要迁移数据库模型。如需进一步参考,请阅读 django 迁移 here
heroku中的数据库迁移就是这样完成的,在Procfile文件中指定行。
release: python manage.py migrate
web: gunicorn col.wsgi --log-file -
因此,无论何时部署新代码,heroku 都会自动迁移数据库模型。如需进一步参考,请检查 。
让我知道这是否有效。
我有一个类似的问题,也是针对 Heroku 应用程序上的 Django。我通过环境变量 DATABASE_URL
.
使用了 heroku-django-template to create my app. In settings.py, the default
database is overwritten
您可以在 settings.py 中禁用它或将环境变量设置为正确的值。
我可以在本地机器上访问我的 RDS postresql 数据库没有问题。
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'xxxxxxx',
'USER': 'XXXXXXXX',
'PASSWORD': 'XXXXXXXX',
'HOST': 'XXXXXXrds.amazonaws.com',
'PORT': '5432',
}
我将其推送到 Heroku,然后在 /saferdb/query/
处收到 ProgrammingError在 heroku 的 manage.py shell 中,我尝试访问数据库:
>>> from saferdb.models import Question
>>> q = Question.objects.all()
>>> q.count()
出现以下错误:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "saferdb_question" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "saferdb_question"
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 387, in count
return self.query.get_count(using=self.db)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/query.py", line 491, in get_count
number = obj.get_aggregation(using, ['__count'])['__count']
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/query.py", line 476, in get_aggregation
result = compiler.execute_sql(SINGLE)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1063, in execute_sql
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "saferdb_question" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "saferdb_question"
^
错误是因为 table 不存在于数据库中,这意味着您需要迁移数据库模型。如需进一步参考,请阅读 django 迁移 here
heroku中的数据库迁移就是这样完成的,在Procfile文件中指定行。
release: python manage.py migrate
web: gunicorn col.wsgi --log-file -
因此,无论何时部署新代码,heroku 都会自动迁移数据库模型。如需进一步参考,请检查
我有一个类似的问题,也是针对 Heroku 应用程序上的 Django。我通过环境变量 DATABASE_URL
.
default
database is overwritten
您可以在 settings.py 中禁用它或将环境变量设置为正确的值。