I'm fairly new at testing and while trying to run test for my django project using python manage.py test
i end up getting django.db.utils.OperationalError: no such table: accounts_user
.
I have the User model within the accounts app and the accounts app has been added to installed app within my settings.py
I've run makemgirations and migrate for all project apps, i've also gone into the project shell and try creating a user from User model in the accounts app and all of these works well, but when i run my test i get an error.
below is the test i'm trying to run which generates the error
from django.test import TestCase
from django.contrib.auth import get_user_model
class UserTestCase(TestCase):
def setUp(self):
USER = get_user_model()
USER.objects.create(
email="johndoe@example.com", first_name="John", last_name="Doe"
)
USER.objects.create(
email="janedoe@example.com", first_name="Jane", last_name="Doe"
)
def test_user_full_name(self):
""" A user's fullname correctly identified """
jane = USER.objects.get(email="janedoe@example.com")
john = USER.objects.get(email="johndoe@example.com")
self.assertEqual(jane.get_full_name(), "Jane Doe")
self.assertEqual(john.get_full_name(), "John Doe")
and below is the code i ran within project shell that works
from django.contrib.auth import get_user_model
USER = get_user_model()
USER.objects.create(
email="johndoe@example.com", first_name="John", last_name="Doe"
)
USER.objects.create(
email="janedoe@example.com", first_name="Jane", last_name="Doe"
)
USER.objects.all()
# returns both object that has been added
and below is my database settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'TEST': {
'test_NAME': 'test_db',
}
}
}
Please how can this be resolved
Below is the full stack trace
$ python manage.py test
Creating test database for alias 'default'...
Traceback (most recent call last):
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\db\backends\utils.py", line 82, in _execute
return self.cursor.execute(sql)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\db\backends\sqlite3\base.py", line 381, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: no such table: accounts_user
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\core\management\commands\test.py", line 23, in run_from_argv
super().run_from_argv(argv)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\core\management\commands\test.py", line 53, in handle
failures = test_runner.run_tests(test_labels)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\test\runner.py", line
629, in run_tests
old_config = self.setup_databases(aliases=databases)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\test\runner.py", line
554, in setup_databases
self.parallel, **kwargs
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\test\utils.py", line 174, in setup_databases
serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\db\backends\base\creation.py", line 72, in create_test_db
run_syncdb=True,
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\core\management\__init__.py", line 148, in call_command
return command.execute(*args, **defaults)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle
self.sync_apps(connection, executor.loader.unmigrated_apps)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\core\management\commands\migrate.py", line 341, in sync_apps
self.stdout.write(" Running deferred SQL...\n")
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\db\backends\sqlite3\schema.py", line 34, in __exit__
self.connection.check_constraints()
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\db\backends\sqlite3\base.py", line 341, in check_constraints
column_name, referenced_column_name,
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\db\backends\utils.py", line 82, in _execute
return self.cursor.execute(sql)
File "C:\Users\i-am-prinx\.virtualenvs\ProjectName-2t3W9LfY\lib\site-packages\django\db\backends\sqlite3\base.py", line 381, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: no such table: accounts_user
Tools and versions django v 2.2 python v 3.6
This worked for me;
Delete all the migration files and then run
python .\manage.py migrate --run-syncdb
It should work.
The main cause of this issue was because an app within the project had a model with a foreignkey field to the User
model and this said app was not having migrations
folder and the models in the app (including those having foreignkey reference to the User
model ) was not migrated (note that this said app is in installed app). I resolved this issue by following these steps
** steps and explanation of what i noticed**
I deleted the db.sqlite3
file on my project root
I deleted the migrations directory on all apps ( not all apps had migrations folder though i ran python manage.py makemigrations
, reason for this i really don't know ). The only app that was having migrations folder was the accounts
app.
I then explicitly ran makemigrations
for all apps ( test worked even without explicitly running migrate command, so far makemigrations
has been run) and then i ran python manage.py migrate
command
** for the above code **
class UserTestCase(TestCase):
def setUp(self):
USER = get_user_model() # <--- doing this here generates error when using USER.get() in the test_... methods
USER.objects.create(
email="johndoe@example.com", first_name="John", last_name="Doe"
)
USER.objects.create(
email="janedoe@example.com", first_name="Jane", last_name="Doe"
)
def test_user_full_name(self):
""" A user's fullname correctly identified """
jane = USER.objects.get(email="janedoe@example.com")
john = USER.objects.get(email="johndoe@example.com")
self.assertEqual(jane.get_full_name(), "Jane Doe")
self.assertEqual(john.get_full_name(), "John Doe")
The above code will fail and the correct code should be
USER = get_user_model()
class UserTestCase(TestCase):
def setUp(self):
USER.objects.create(
email="johndoe@example.com", first_name="John", last_name="Doe"
)
USER.objects.create(
email="janedoe@example.com", first_name="Jane", last_name="Doe"
)
def test_user_full_name(self):
""" A user's fullname correctly identified """
jane = USER.objects.get(email="janedoe@example.com")
john = USER.objects.get(email="johndoe@example.com")
self.assertEqual(jane.get_full_name(), "Jane Doe")
self.assertEqual(john.get_full_name(), "John Doe")
I hope this is able to help someone
Try setting your database settings like so (this depends on which django version you are using):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
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.