[英]How can I solve application error on heroku?
I am trying to create an app on django and I am trying to host it on heroku. Everything runs perfectly locally.我正在尝试在 django 上创建一个应用程序,并试图在 heroku 上托管它。一切都在本地完美运行。 I get an error while trying to migrate the database onto the heroku app by running command -
尝试通过运行命令将数据库迁移到 heroku 应用程序时出现错误 -
heroku run python manage.py migrate
This is the error I am getting -这是我得到的错误 -
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 211, in connect
self.connection = self.get_new_connection(conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection
connection = Database.connect(**conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/__init__.py", line 121, in connect
dsn = _ext.make_dsn(dsn, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/extensions.py", line 167, in make_dsn
parse_dsn(dsn)
psycopg2.ProgrammingError: invalid dsn: invalid connection option "init_command"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/manage.py", line 22, in <module>
main()
File "/app/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 425, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 373, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 417, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 90, in wrapped
res = handle_func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 75, in handle
self.check(databases=[database])
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 438, in check
all_issues = checks.run_checks(
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/registry.py", line 77, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/model_checks.py", line 34, in check_all_models
errors.extend(model.check(**kwargs))
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 1307, in check
*cls._check_indexes(databases),
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 1699, in _check_indexes
connection.features.supports_covering_indexes or
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/features.py", line 84, in is_postgresql_11
return self.connection.pg_version >= 110000
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 339, in pg_version
with self.temporary_connection():
File "/app/.heroku/python/lib/python3.9/contextlib.py", line 119, in __enter__
return next(self.gen)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 614, in temporary_connection
with self.cursor() as cursor:
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 270, in cursor
return self._cursor()
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 246, in _cursor
self.ensure_connection()
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 211, in connect
self.connection = self.get_new_connection(conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection
connection = Database.connect(**conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/__init__.py", line 121, in connect
dsn = _ext.make_dsn(dsn, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/extensions.py", line 167, in make_dsn
parse_dsn(dsn)
django.db.utils.ProgrammingError: invalid dsn: invalid connection option "init_command"
settings.py -设置.py -
prod_db = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(prod_db)
settings.py ( database section ) - settings.py(数据库部分)-
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '****',
'USER': '****',
'PASSWORD': '****',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
}
How can I solve this issue?我该如何解决这个问题? Please ask if I need to add any of my files onto the question.
请询问我是否需要将我的任何文件添加到问题中。
You are using MySQL for local development and PostgreSQL in production.您在本地开发中使用MySQL ,在生产中使用 PostgreSQL。 The PostgreSQL connection is coming from the
DATABASE_URL
environment variable that dj_database_url
uses by default. PostgreSQL 连接来自
dj_database_url
默认使用的DATABASE_URL
环境变量。
Since you are updating your default database from the environment (not replacing it), the MySQL-specific init_command
option is being left in your PostgreSQL configuration.由于您是从环境更新默认数据库(而不是替换它),因此 PostgreSQL 配置中保留了 MySQL 特定的
init_command
选项。 The psycopg2
PostgreSQL driver doesn't know what to do with it. psycopg2
PostgreSQL 驱动程序不知道如何处理它。
A quick and dirty solution would be to completely your default database configuration instead of updating it:一个快速而肮脏的解决方案是完全使用默认数据库配置而不是更新它:
DATABASES['default'] = prod_db
But I strongly urge you to use the same database server for development and production.但我强烈建议您使用同一台数据库服务器进行开发和生产。 As you have discovered, database engines aren't drop-in replacements for each other.
正如您所发现的,数据库引擎并不是彼此的直接替代品。 In this case, it's likely easiest to switch from PostgreSQL to MySQL on Heroku.
在这种情况下,在 Heroku 上从 PostgreSQL 切换到 MySQL 可能是最简单的。
First, let's get rid of the Postgres stuff:首先,让我们摆脱 Postgres 的东西:
psycopg2
from your dependencies.psycopg2
。 Its presence likely caused Heroku to automatically provision a PostgreSQL database for you , which added to the confusion. Now, provision a MySQL server.现在,提供一个 MySQL 服务器。 I suggest you pick from the available addons .
我建议您从可用的插件中挑选。 There are a few MySQL options, including JawsDB and ClearDB.
有几个 MySQL 选项,包括 JawsDB 和 ClearDB。 Here is an example using ClearDB :
这是一个使用 ClearDB 的示例:
heroku addons:create cleardb:ignite
Ignite is the free tier for ClearDB. Ignite 是 ClearDB 的免费层。 Once provisioned, your app will see a new environment variable
CLEARDB_DATABASE_URL
.配置后,您的应用程序将看到一个新的环境变量
CLEARDB_DATABASE_URL
。 Other addons will use other environment variable names, so make sure to read their documentation.其他插件将使用其他环境变量名称,因此请务必阅读它们的文档。
Finally, update your Django configuration.最后,更新您的 Django 配置。
dj_database_url
uses the DATABASE_URL
environment variable by default, but we can tell it to use another : dj_database_url
默认使用DATABASE_URL
环境变量,但我们可以告诉它使用另一个:
prod_db = dj_database_url.config(env="CLEARDB_DATABASE_URL", conn_max_age=500)
DATABASES['default'].update(prod_db)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.