简体   繁体   中英

Django to Heroku, How to migrate sqlite3 data to postgres

I tried to host a website on heroku but I keep getting this error

File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) django.db.utils.ProgrammingError: relation "home_product" does not exist LINE 1: ...home_product"."price", "home_product"."slug" FROM "home_prod...

whenever I tried to use

heroku run python
manage.py migrate -a appname

I mainly used this video as a reference to hosting.

and also this StackOverflow question and this article didn't shed much light on the issue.

Here's my settings.py

"""
Django settings for Corbett_Jewelry project.

Generated by 'django-admin startproject' using Django 3.1.7.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""


from pathlib import Path
import os
import django_heroku
import dj_database_url
from decouple import config
import psycopg2

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ''

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('DJANGO_DEBUG', '') != 'False'


ALLOWED_HOSTS = ['127.0.0.1']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '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',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

ROOT_URLCONF = 'Corbett_Jewelry.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'Corbett_Jewelry.wsgi.application'

# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)

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

django_heroku.settings(locals())

My models.py :

from django.db import models


class Product(models.Model):
    name = models.CharField(max_length=100)
    image = models.ImageField(blank=True)
    desc = models.TextField()
    price = models.IntegerField()
    slug = models.SlugField(unique=True, default=None)

    def __str__(self):
        return self.name

class Images(models.Model):
    product = models.ForeignKey(Product, default=None, on_delete=models.CASCADE)
    img = models.ImageField(upload_to='images/')

    def __str__(self):
        return self.product.name

    
class Beads(models.Model):
    name = models.CharField(max_length=200)
    price = models.IntegerField()
    img = models.ImageField(upload_to='beads/')

My Procfile:

web: gunicorn Corbett_Jewelry.wsgi

My requirements.txt :

asgiref==3.4.1
cycler==0.10.0
dj-database-url==0.5.0
Django==3.2.9
django-heroku==0.3.1
gunicorn==20.1.0
kiwisolver==1.3.2
matplotlib==3.4.3
numpy==1.21.2
Pillow==8.3.2
psycopg2==2.9.2
pyparsing==2.4.7
python-dateutil==2.8.2
python-decouple==3.5
pytz==2021.3
selenium==3.141.0
six==1.16.0
sqlparse==0.4.2
urllib3==1.26.7
whitenoise==5.3.0

I don't know what else information you might need to solve this. I'm happy to provide any info since I've been banging my head over this for a week now.

@Gamingapple... as you asked in the comments, I will attempt to provide all possible solutions I can think of for your issue.

I have never use Heroku, but here are the first solutions I would try :

1. A quick read from Heroku's help docs is showing you perhaps are not running migrations in the way that Heroku thinks are best practices for migrations: https://help.heroku.com/GDQ74SU2/django-migrations there (more-or-less) means you should be running your migrations locally, pushing up to Git, and then Heroku will automate the running of your migrations on deploy where it states:

Then, add the following as the first line in your Procfile:

release: python manage.py migrate

But note that this also means you should be using Postgres locally in development, not SQLite (as your title suggests).

-- Along with this, it appears you are trying to run migrations by app when you do: manage.py migrate -a appname , Django does have some regular migrations that need to be run so I'd just try manage.py migrate

2. You noted in the comment that dj_database_url.config() from PyPi's dj_database_url package: https://pypi.org/project/dj-database-url/ is returning an empty dictionary. To me, without extra info, I would say this is where your problem is when you manually try to run migrate in your Heroku instance; there is no database to find or connect to. To fix this I would remove the use of dj_database_url (as I have not used it either and we are debugging), and manually write my connection string for Django's DATABASES , like so:

pip install psycopg2 or just ensure it is in requirements/installed.

Change your DATABASES attr in settings to be manually written so you know what it is (we are debugging so perhaps after you get it up you make the password gathering more secure, like as an env variable)

DATABASES = {
   'default': {
       'ENGINE': 'django.db.backends.postgresql',
       'NAME': '[HEROKU DB NAME]',
       'USER': '[HEROKU DB USERNAME]',
       'PASSWORD': '[HEROKU PASSWORD]',
       'HOST': '{HEROKU HOST IP OR NAME FOR YOUR DB]',
       'PORT': '[HEROKU DB PORT]',
   }
}

Now with that correct, run the migration commands:

python manage.py makemigrations

python manage.py migrate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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