gpt4 book ai didi

python manage.py test : django. db.utils.OperationalError:没有这样的表:accounts_user

转载 作者:行者123 更新时间:2023-12-02 16:54:18 25 4
gpt4 key购买 nike

我在测试方面相当陌生,并且尝试使用 python manage.py test 为我的 django 项目运行测试。我最终得到django.db.utils.OperationalError: no such table: accounts_user

我在帐户应用程序中有用户模型,并且帐户应用程序已添加到我的 settings.py 中已安装的应用程序中

我已经为所有项目应用程序运行了 makemgirations 和迁移,我还进入了项目 shell 并尝试从帐户应用程序中的用户模型创建用户,所有这些都运行良好,但是当我运行测试时,我出现错误。

下面是我尝试运行的测试,它会生成错误

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="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="caa0a5a2a4aea5af8aafb2aba7baa6afe4a9a5a7" rel="noreferrer noopener nofollow">[email protected]</a>", first_name="John", last_name="Doe"
)
USER.objects.create(
email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0dad1ded5d4dfd5f0d5c8d1ddc0dcd59ed3dfdd" rel="noreferrer noopener nofollow">[email protected]</a>", first_name="Jane", last_name="Doe"
)

def test_user_full_name(self):
""" A user's fullname correctly identified """
jane = USER.objects.get(email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb818a858e8f848eab8e938a869b878ec5888486" rel="noreferrer noopener nofollow">[email protected]</a>")
john = USER.objects.get(email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="670d080f0903080227021f060a170b024904080a" rel="noreferrer noopener nofollow">[email protected]</a>")

self.assertEqual(jane.get_full_name(), "Jane Doe")
self.assertEqual(john.get_full_name(), "John Doe")

下面是我在项目 shell 中运行的有效代码

from django.contrib.auth import get_user_model
USER = get_user_model()
USER.objects.create(
email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="543e3b3c3a303b3114312c35392438317a373b39" rel="noreferrer noopener nofollow">[email protected]</a>", first_name="John", last_name="Doe"
)
USER.objects.create(
email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e88289868d8c878da88d90898598848dc68b8785" rel="noreferrer noopener nofollow">[email protected]</a>", first_name="Jane", last_name="Doe"
)

USER.objects.all()

# returns both object that has been added

下面是我的数据库设置

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

请问如何解决

下面是完整的堆栈跟踪

$ 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

工具和版本Django v 2.2python v 3.6

最佳答案

此问题的主要原因是项目中的某个应用程序的模型具有 User 模型的外键字段,并且该应用程序没有 migrations 文件夹并且应用程序中的模型(包括那些具有对 User 模型的外键引用的模型)未迁移(请注意,该应用程序位于已安装的应用程序中)。我按照以下步骤解决了这个问题

**步骤和我注意到的解释**

  • 我删除了项目根目录中的 db.sqlite3 文件

    • 我删除了所有应用程序上的迁移目录(尽管我运行了python manage.py makemigrations,但并非所有应用程序都有迁移文件夹,其原因我真的不知道)。唯一具有迁移文件夹的应用是 accounts 应用。

    • 然后,我为所有应用显式运行 makemigrations (即使没有显式运行 migrate 命令,测试也能正常工作,到目前为止 makemigrations 已运行),然后我运行 python管理.py迁移命令

** 对于上面的代码 **

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="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f09a9f989e949f95b09588919d809c95de939f9d" rel="noreferrer noopener nofollow">[email protected]</a>", first_name="John", last_name="Doe"
)
USER.objects.create(
email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9df7fcf3f8f9f2f8ddf8e5fcf0edf1f8b3fef2f0" rel="noreferrer noopener nofollow">[email protected]</a>", first_name="Jane", last_name="Doe"
)

def test_user_full_name(self):
""" A user's fullname correctly identified """
jane = USER.objects.get(email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85efe4ebe0e1eae0c5e0fde4e8f5e9e0abe6eae8" rel="noreferrer noopener nofollow">[email protected]</a>")
john = USER.objects.get(email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83e9ecebede7ece6c3e6fbe2eef3efe6ade0ecee" rel="noreferrer noopener nofollow">[email protected]</a>")

self.assertEqual(jane.get_full_name(), "Jane Doe")
self.assertEqual(john.get_full_name(), "John Doe")

上面的代码会失败,正确的代码应该是

USER = get_user_model() 
class UserTestCase(TestCase):
def setUp(self):
USER.objects.create(
email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="79131611171d161c391c01181409151c571a1614" rel="noreferrer noopener nofollow">[email protected]</a>", first_name="John", last_name="Doe"
)
USER.objects.create(
email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8a2a9a6adaca7ad88adb0a9a5b8a4ade6aba7a5" rel="noreferrer noopener nofollow">[email protected]</a>", first_name="Jane", last_name="Doe"
)

def test_user_full_name(self):
""" A user's fullname correctly identified """
jane = USER.objects.get(email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0aaa1aea5a4afa580a5b8a1adb0aca5eea3afad" rel="noreferrer noopener nofollow">[email protected]</a>")
john = USER.objects.get(email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="204a4f484e444f45604558414d504c450e434f4d" rel="noreferrer noopener nofollow">[email protected]</a>")

self.assertEqual(jane.get_full_name(), "Jane Doe")
self.assertEqual(john.get_full_name(), "John Doe")

我希望这能够帮助别人

关于python manage.py test : django. db.utils.OperationalError:没有这样的表:accounts_user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57092854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com