简体   繁体   English

在 Heroku 部署期间无法填充数据库表?

[英]Cannot populate database table during Heroku deployment?

I have a django app that runs perfectly well on my local server.我有一个 django 应用程序,它在我的本地服务器上运行良好。 Unfortunately, when I deploy it onto Heroku, the behaviour doesn't work as expected.不幸的是,当我将它部署到 Heroku 上时,该行为没有按预期工作。 I can see that it's a database related issue as my dropdowns which are supposed to load a list of values from my database are empty in production.我可以看到这是一个与数据库相关的问题,因为我的下拉列表应该从我的数据库加载值列表在生产中是空的。

I have checked on Heroku in the 'Ressources' section of my application and there is a database;我已经在我的应用程序的“资源”部分检查了 Heroku,并且有一个数据库; it also contains all my project tables (django default tables + my main table).它还包含我所有的项目表(django 默认表 + 我的主表)。 All the django default tables are properly populated (I compared them with my local postgres tables and it matched).所有 django 默认表都已正确填充(我将它们与本地 postgres 表进行了比较并且匹配)。 Unfortunately, my main table is empty on Heroku, whereas it is populated as expected locally.不幸的是,我的主表在 Heroku 上是空的,而它在本地按预期填充。 I'm not sure to understand what has gone wrong, especially considering that the table itself has been recognized, yet it is empty.我不确定出了什么问题,特别是考虑到表本身已被识别,但它是空的。 I've applied the command heroku run python manage.py migrate but it didn't fix the issue.我已经应用了命令heroku run python manage.py migrate但它没有解决问题。 My guess is that my database settings are correct for local deployment but not for Heroku deployment.我的猜测是我的数据库设置适用于本地部署,但不适用于 Heroku 部署。 Nevertheless, I'm not sure at all.尽管如此,我完全不确定。 I've tried replacing my db settings by the user/host/password/name provided by Heroku but it didn't work.我试过用 Heroku 提供的用户/主机/密码/名称替换我的数据库设置,但没有用。 Any guidance will be much appreciated.任何指导将不胜感激。

Here is my settings.py:这是我的settings.py:

import dj_database_url
import dotenv

dotenv_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(dotenv_file):
    dotenv.load_dotenv(dotenv_file)

ALLOWED_HOSTS = ['0.0.0.0', 'localhost', '127.0.0.1', 'myapp.herokuapp.com']

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'myappdb',
        'USER': 'postgres',
        'PASSWORD': 'MyAppPassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env) 

I managed to figure it out.我设法弄明白了。 I had to follow these steps: 1/ Create a dumps folder in my application folder 2/ Run the following command to dump my data in json format in the newly created folder: python manage.py dumpdata > myapp/dumps/myapp.json 3/ Then Run heroku run python manage.py loaddata myapp/dumps/myapp.json我必须按照以下步骤操作: 1/ 在我的应用程序文件夹中创建一个dumps文件夹 2/ 运行以下命令将我的数据以 json 格式转储到新创建的文件夹中: python manage.py dumpdata > myapp/dumps/myapp.json 3 / 然后Run heroku run python manage.py loaddata myapp/dumps/myapp.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM