Django - Able to makemigrations and migrate locally but not on Heroku

Viewed 226

There seems to be a problem with migrating my models on the Heroku server. I am able to run python manage.py makemigrations and python manage.py migrate just fine and I am able to view my site on my local machine (SQLite), but when I try to run migrations on Heroku (PostgreSQL) I get the following error:

remote: Traceback (most recent call last):
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
remote:     return self.cursor.execute(sql, params)
remote: psycopg2.errors.UndefinedTable: relation "portfolio_category" does not exist
remote: LINE 1: ...ategory"."name", "portfolio_category"."name" FROM "portfolio...
remote:                                                              ^
remote:
remote:
remote: The above exception was the direct cause of the following exception:
remote:
remote: Traceback (most recent call last):
remote:   File "/app/manage.py", line 22, in <module>
remote:     main()
remote:   File "/app/manage.py", line 18, in main
remote:     execute_from_command_line(sys.argv)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
remote:     utility.execute()
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
remote:     self.fetch_command(subcommand).run_from_argv(self.argv)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
remote:     self.execute(*args, **cmd_options)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
remote:     output = self.handle(*args, **options)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 85, in wrapped
remote:     res = handle_func(*args, **kwargs)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 75, in handle
remote:     self.check(databases=[database])
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 392, in check
remote:     all_issues = checks.run_checks(
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/registry.py", line 70, in run_checks
remote:     new_errors = check(app_configs=app_configs, databases=databases)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/urls.py", line 13, in check_url_config
remote:     return check_resolver(resolver)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/urls.py", line 23, in check_resolver
remote:     return check_method()
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/urls/resolvers.py", line 408, in check
remote:     for pattern in self.url_patterns:
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
remote:     res = instance.__dict__[self.name] = self.func(instance)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/urls/resolvers.py", line 589, in url_patterns
remote:     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
remote:     res = instance.__dict__[self.name] = self.func(instance)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/urls/resolvers.py", line 582, in urlconf_module
remote:     return import_module(self.urlconf_name)
remote:   File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
remote:     return _bootstrap._gcd_import(name[level:], package, level)
remote:   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
remote:   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
remote:   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
remote:   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
remote:   File "<frozen importlib._bootstrap_external>", line 850, in exec_module
remote:   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
remote:   File "/app/shumayl_site/urls.py", line 24, in <module>
remote:     path('', include('portfolio.urls')),
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/urls/conf.py", line 34, in include
remote:     urlconf_module = import_module(urlconf_module)
remote:   File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
remote:     return _bootstrap._gcd_import(name[level:], package, level)
remote:   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
remote:   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
remote:   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
remote:   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
remote:   File "<frozen importlib._bootstrap_external>", line 850, in exec_module
remote:   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
remote:   File "/app/portfolio/urls.py", line 3, in <module>
remote:     from .views import HomeView, BlogView, ArticleDetailsView, AddPostView, UpdatePostView, DeletePostView, AddCategoryView, CategoryView
remote:   File "/app/portfolio/views.py", line 4, in <module>
remote:     from .forms import PostForm, EditForm
remote:   File "/app/portfolio/forms.py", line 9, in <module>
remote:     for item in choices:
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py", line 287, in __iter__
remote:     self._fetch_all()
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py", line 1308, in _fetch_all
remote:     self._result_cache = list(self._iterable_class(self))
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py", line 142, in __iter__
remote:     return compiler.results_iter(tuple_expected=True, chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1108, in results_iter
remote:     results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1156, in execute_sql
remote:     cursor.execute(sql, params)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
remote:     return super().execute(sql, params)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
remote:     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
remote:     return executor(sql, params, many, context)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
remote:     return self.cursor.execute(sql, params)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
remote:     raise dj_exc_value.with_traceback(traceback) from exc_value
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
remote:     return self.cursor.execute(sql, params)
remote: django.db.utils.ProgrammingError: relation "portfolio_category" does not exist
remote: LINE 1: ...ategory"."name", "portfolio_category"."name" FROM "portfolio...

And here is my models.py:

from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from datetime import datetime, date
from ckeditor.fields import RichTextField

# Create your models here.

class Category(models.Model):
    name = models.CharField(max_length=255)
    
    def __str__(self):
        return self.name
    
    def get_absolute_url(self):
        return reverse('blog')

class Post(models.Model):
    title = models.CharField(max_length=255)
    header_image = models.ImageField(null=True, blank=True, upload_to='images/')
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    body = RichTextField(blank=True, null=True)
    # body = models.TextField()
    post_date = models.DateTimeField(auto_now_add=True)
    category = models.CharField(max_length=255, default='Uncategorized')
    snippet = models.CharField(max_length=255)
    
    def __str__(self):
        return self.title + ' | ' + str(self.author)
    
    def get_absolute_url(self):
        return reverse('blog')
        # return reverse('article-details', args=(str(self.id)))

Here is what I have tried so far:

  1. I have tried to run the migrations during the release phase as recommended by this StackOverflow thread. I did this by adding release: python manage.py migrate --noinput in the Procfile.
  2. I have also tried to clear all my migration files, reset my database on Heroku, run python manage.py makemigrations locally and run heroku run manage.py migrate on Heroku.
  3. I have tried removing all entries in the Categorys and Posts models I have made throughout production using the Django superuser/admin interface on my local server.

I appreciate any pointers you could give me and if you require any further information, please let me know. Thank you in advance for your help and guidance.

0 Answers
Related