回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我在测试方面是个新手,在尝试使用<code>python manage.py test</code>为我的django项目运行测试时,我得到了<code>django.db.utils.OperationalError: no such table: accounts_user</code>。在</p>
<p>我有帐户应用程序中的用户模型,并且帐户应用程序已添加到我的已安装应用程序中设置.py在</p>
<p>我为所有的项目应用运行了makemgirations和migrate,我也进入了projectshell,尝试在accounts应用程序中从用户模型创建一个用户,所有这些都很好,但是当我运行我的测试时,我得到了一个错误。在</p>
<p>下面是我试图运行的测试,它会产生错误</p>
<pre class="lang-py prettyprint-override"><code>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")
</code></pre>
<p>下面是我在projectshell中运行的代码</p>
^{pr2}$
<p>下面是我的数据库设置</p>
<pre class="lang-py prettyprint-override"><code>DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'TEST': {
'test_NAME': 'test_db',
}
}
}
</code></pre>
<p><strong>请如何解决这一问题</strong></p>
<p>下面是完整的堆栈跟踪</p>
<pre><code>$ 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
</code></pre>
<p><strong>工具和版本</strong>
django 2.2版
python 3.6版</p>