Django Test Error: relation does not exist
Asked Answered
N

3

8

I'm using Django 1.6 with Python 3.3 on Ubuntu 13.10 and Postgres.

I have a model User defined as follows:

from django.db import models
from django.contrib.auth.models import AbstractUser

from common.mixins import TimestampMixin


class User(TimestampMixin, AbstractUser):
    auth_key = models.CharField(max_length=16)

    def __str__(self):
        if self.first_name:
            if self.last_name:
                return "{0} {1}'s Profile".format(
                    self.first_name, self.last_name)
            else:
                return "{0}'s Profile".format(self.first_name)
        else:
            return "{0}'s Profile".format(self.username)

I have a test for __str __ as follows:

from django.test import TestCase

from accounts.models import User
from common.tests.fixtureless import create_instance


class UserTest(TestCase):
    def test_str(self):
        initial = {
            'username': 'test_username',
            'first_name': 'test_first_name',
            'last_name': 'test_last_name',
        }
        user = create_instance(User, **initial)
        user.save()
        expected = "test_first_name test_last_name's Profile"
        self.assertEqual(user.__str__(), expected)
        user.delete()

        del(initial['last_name'])
        user = create_instance(User, **initial)
        user.save()
        expected = "test_first_name's Profile"
        self.assertEqual(user.__str__(), expected)
        user.delete()

        del(initial['first_name'])
        user = create_instance(User, **initial)
        user.save()
        expected = "test_username's Profile"
        self.assertEqual(user.__str__(), expected)
        user.delete()

I can run my test suite properly about 20% of the time. Sometimes, however, I receive the following failure:

ERROR: test_str (blind_tiger.blind_tiger.apps.accounts.tests.UserTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "accounts_user" does not exist
LINE 1: INSERT INTO "accounts_user" ("password", "last_login", "is_s...
                    ^


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

Traceback (most recent call last):
  File "/home/ricomoss/workspace/ses/blind_tiger/blind_tiger/settings/../apps/accounts/tests/models.py", line 18, in test_str
    user.save()
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/models/base.py", line 545, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/models/base.py", line 573, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/models/base.py", line 654, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/models/base.py", line 687, in _do_insert
    using=using, raw=raw)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/models/manager.py", line 232, in _insert
    return insert_query(self.model, objs, fields, **kwargs)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/models/query.py", line 1511, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/models/sql/compiler.py", line 898, in execute_sql
    cursor.execute(sql, params)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/utils/six.py", line 490, in reraise
    raise value.with_traceback(tb)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "accounts_user" does not exist
LINE 1: INSERT INTO "accounts_user" ("password", "last_login", "is_s...

It's frustrating because if I put a try/except around the offending user.save() so I can inspect the object I never see this error.

Never yields this error:

try:
    user.save()
except:
    raise Exception(user.__dict__)

Any suggestions?

Neuritis answered 4/12, 2013 at 23:29 Comment(4)
I'm not sure what the cause of the error is, but as an aside, you should be defining a __str__ method with Python 3, not __unicode__.Vestige
As a test it may be worth trying to rename your user implementation to something else, just in case Django is somehow confusing this with the User class in django.contrib.auth.models (not that it should be...)Explicate
@Explicate This does not seem to be the issue. I renamed to BaseUser and I still get the same error. I would have been very surprised if that was the problem.Neuritis
@rico Do you use South for migrations ?Menes
H
6

Try running the following commands:

python manage.py migrate --run-syncdb
python manage.py makemigrations appname
python manage.py migrate
Houphouetboigny answered 14/8, 2020 at 11:28 Comment(1)
including app name is like rule.Bonney
O
4

I solved the problem. Try to run first:

python manage.py syncdb
Ophthalmology answered 3/11, 2014 at 7:37 Comment(3)
Don't know why you lost a point for that comment but I had the same problem and that fixed it for me as well. Although for django 1.7 up use migrate instead of syncdb.Delila
Actually, manage.py test should not require running migrate because it works on a different - the test database - and should run migrate automatically on that test database. I have the same issue however (tests fail when the regular database - which should have nothing to do with the test dabase - is empty), so annoyingly, this is a solution for whatever reason.Eosin
I had this message Unknown command: 'syncdb'. Did you mean syncdata?Extractor
S
-1

If this error is in docker To delete a volume in Docker on "Windows" or "Ubuntu", you can use the docker volume rm command. Follow these steps: Open a command prompt or PowerShell window. List all existing volumes using the following command:

docker volume ls

This will display a list of volumes along with their names. Identify the volume you want to delete and copy its name. Use the following command to remove the specified volume:

docker volume rm <volume_name>

Replace <volume_name> with the actual name of the volume you want to delete. For example:

docker volume rm my_volume

This command will remove the specified volume.

Keep in mind that you cannot remove a volume if there are still containers using it. Ensure that no containers are currently using the volume you want to delete. If there are, you may need to stop or remove those containers first.

Additionally, if the volume is mounted as a bind mount in a container, ensure that the container is not running before attempting to remove the volume.

Suh answered 24/1 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.