django.db.utils.ProgrammingError: relation "auth_permission" does not exist

Viewed 1886

I am having a working sqlite database in my local environment. On heroku I am using psql. But I am getting following error when I try to run

$ heroku run python manage.py migrate
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 "auth_permission" does not exist
LINE 1: ...ntent_type_id", "auth_permission"."codename" FROM "auth_perm...
                                                             ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 350, in execute
    self.check()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 379, in check
    include_deployment_checks=include_deployment_checks,
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 366, in _run_checks
    return checks.run_checks(**kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/urls/resolvers.py", line 533, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
    return import_module(self.urlconf_name)
  File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/app/codeTrial/urls.py", line 20, in <module>
    path('', include('coderunner.urls')),
  File "/app/.heroku/python/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/app/coderunner/urls.py", line 4, in <module>
    from . import views
  File "/app/coderunner/views.py", line 34, in <module>
    'perm_obj': Permission.objects.get(name='Can add Questions'),
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 393, in get
    num = len(clone)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 250, in __len__
    self._fetch_all()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 54, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1065, in execute_sql
    cursor.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 "auth_permission" does not exist
LINE 1: ...ntent_type_id", "auth_permission"."codename" FROM "auth_perm...
                                                             ^

This is not a problem in using psql on heroku I guess, because when I try to copy the same application to another Linux machine and try to recreate sqlite database it gives sqlite3.OperationalError: no such table: auth_permission.

Same error occurs when I try migration after deleting all the 14 migration files from the app/migration folder.

I don't get why at least Django database tables are not getting created. Is it due to anything wrong in the app/models.py file?

In case you need to review my models.py file, here it is:

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.validators import MinValueValidator


# Create your models here.
class Questions(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    published_on = models.DateTimeField(auto_now_add=True)
    question_text = models.CharField(max_length=200)
    question_desc = models.CharField(max_length=500)
    pre_code_snippet = models.TextField()
    output_format = models.CharField(max_length=500)
    timeout = models.IntegerField(default=5, validators=[MinValueValidator(0)])
    run_testcase1_input = models.TextField(max_length=50)
    run_testcase1_output = models.TextField()
    submit_testcase1_input = models.TextField(max_length=50)
    submit_testcase1_output = models.TextField()
    submit_testcase2_input = models.TextField(max_length=50)
    submit_testcase2_output = models.TextField()
    times_submitted = models.IntegerField(default=0)
    times_correct = models.IntegerField(default=0)
    times_wrong = models.IntegerField(default=0)
    score = models.IntegerField(default=10,
                                validators=[MinValueValidator(5)])

    class Meta:
        verbose_name = 'Questions'
        verbose_name_plural = 'Questions'

    def __str__(self):
        return self.question_text


class Submissions(models.Model):
    username = models.ForeignKey(User, on_delete=models.CASCADE)
    submitted_on = models.DateTimeField(auto_now_add=True)
    submitted_snippet = models.TextField()
    question = models.ForeignKey(Questions, on_delete=models.CASCADE)

    class Meta:
        verbose_name = 'Submissions'
        verbose_name_plural = 'Submissions'

    def __str__(self):
        return (str(self.username) + '@ [' + str(self.question) + ']')


class UserProfile(models.Model):
    username = models.OneToOneField(User, on_delete=models.CASCADE)
    score = models.IntegerField(default=0,
                                validators=[MinValueValidator(0)])

    class Meta:
        verbose_name = "UserProfile"
        verbose_name_plural = "UserProfiles"

    def __str__(self):
        return str(self.username)


@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(username=instance)


@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.userprofile.save()

project/settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'coderunner.apps.CoderunnerConfig',
]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

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

I am using Django_2.1.2. I did not find any proper solution for this issue yet. Let me know if I need to provide any other information. In case you need the full source code, here is my Github link.

2 Answers

Oh yeah, I found the problem. Even after posting my question down here, I was searching for the exact issue, I found a related article where some one has commented there is an issue with his form.py file as per the traceback log. So I thought in my case also there might be some issues in other files beyond my suspected files like models.py, settings.py or migration files. So I checked the Tracebacks again and found this...

File "/app/codeTrial/urls.py", line 20, in <module> path('', include('coderunner.urls')),

which ultimately executes coderunner/urls.py, where I import coderunner/views.py contents.

File "/app/coderunner/urls.py", line 4, in <module>
    from . import views

And in coderunner/views.py I was actually trying to create an object of Permission class outside any view functions/classes. So it is obvious while importing views.py from url.py, views.py gets executed. That means it was accessing the auth model before it was even created, which actually raised the exception auth_permission does not exist. So I handled that code in views.py and tried migrating again... Boom!! Successfully migrated.

In my dev environment there was no issues in migration, because auth model was created by the time I implemented the unhandled code in coderunner/views.py.

  1. You have to comment your root URL routes of all apps
  2. Comment your apps from INSTALLED_APPS list
  3. Run python manage.py migrate. It will migrate the initial migrations of Django itself.
  4. Now uncomment all the comments you've made in the first two steps
Related