[英]python manage.py test: django.db.utils.OperationalError: no such table: accounts_user
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
.我在测试方面相当新,在尝试使用
python manage.py test
为我的 django 项目运行测试时,我最终得到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我在帐户应用程序中有用户模型,并且帐户应用程序已添加到我的 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.我已经为所有项目应用程序运行了 makemgirations 和 migrate,我也进入了项目 shell 并尝试从帐户应用程序中的用户模型创建一个用户,所有这些都运行良好,但是当我运行我的测试时,我收到一个错误.
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下面是我在项目 shell 中运行的代码
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工具和版本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).这个问题的主要原因是因为项目中的一个应用程序有一个带有外键字段到
User
模型的模型,并且这个应用程序没有migrations
文件夹和应用程序中的模型(包括那些具有对User
模型的外键引用的模型)未迁移(请注意,该应用程序位于已安装的应用程序中)。 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我删除了项目根目录上的
db.sqlite3
文件
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 ).我删除了所有应用程序上的迁移目录(虽然我运行了
python manage.py makemigrations
,但并非所有应用程序都有迁移文件夹,我真的不知道原因)。 The only app that was having migrations folder was the accounts
app.唯一具有迁移文件夹的应用程序是
accounts
应用程序。
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然后我为所有应用程序显式运行
makemigrations
(即使没有显式运行 migrate 命令,测试也能工作,到目前为止makemigrations
已经运行),然后我运行了python manage.py migrate
命令
** 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):尝试像这样设置您的数据库设置(这取决于您使用的 Django 版本):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.