gpt4 book ai didi

python - Django - 无法运行 makemigrations : "no such table" even after running reset_db

转载 作者:行者123 更新时间:2023-12-01 03:17:24 25 4
gpt4 key购买 nike

我无法从 django- 运行 makemigrationsmigrate 或其他任何内容(flushreset_db)扩展)如果我的 INSTALLED_APPS 中有某个应用程序。

该应用程序名为 issues 并且有一个模型:

class Issue(models.Model):
title = models.CharField(max_length=255)
description = models.CharField(max_length=1000)
sent = models.BooleanField()

它之前工作正常(makemigrations 和 migrate 运行良好,我可以正确使用应用程序/模型),直到我尝试添加:

severity = models.IntegerField()

并尝试运行 makemigrations。我不再有错误或记得它,但从那时起,即使从模型中删除 severity 之后,一切都被破坏了。

如果我从 settings.py 中删除 issues 应用程序,一切都会正常。

我得到的错误:

madjura@madjura-E6228:~/workspace/budget/src$ python3.5 manage.py makemigrationsTraceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: issues_issue

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 "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 341, in execute
django.setup()
File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/home/madjura/workspace/budget/src/issues/apps.py", line 16, in ready
issues.models.Issue.objects.check_and_send_unsent_issues()
File "/home/madjura/workspace/budget/src/issues/models.py", line 18, in check_and_send_unsent_issues
for issue in issues:
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 256, in __iter__
self._fetch_all()
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 1087, in _fetch_all
self._result_cache = list(self.iterator())
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql()
File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: issues_issue

虽然问题出现在 INSTALLED_APPS 中,但我在运行 migrate、flush 和 reset_db 时遇到了相同的错误。

我尝试运行flush和reset_db,并从INSTALLED_APPS中删除问题,但这并没有解决问题。

我尝试执行上述操作,然后运行 ​​makemigrations 和 migrate,这也不起作用。一旦我将问题放回到 INSTALLED_APPS 中,一切就都坏了。

我该如何解决这个问题?

编辑:

也许相关,问题模型有一个具有以下功能的 Manager:

class IssueManager(models.Manager):
"""Manager for the Issue class."""

def check_and_send_unsent_issues(self):
"""
Checks for unsent Issue objects (Issue.sent = False) and attempts
to send them.
Issues that have been sent are deleted.
If the issue fails to be sent for whatever reason, it is not deleted.
Does nothing if there are no unsent issues.
This method is called once when the server starts.
"""

issues = self.get_queryset().filter(sent=False)
for issue in issues:
try:
make_issue(issue.title, issue.description)
issue.delete()
except PostIssueException:
pass

使用 apps.py 检查未发送的问题并将其发布到 Gitlab 上。

编辑2:

通过注释以下行解决了问题,该行出现在我的 apps.pyready() 中:

    issues.models.Issue.objects.check_and_send_unsent_issues()

这不知何故导致事情破裂,我不明白为什么。有人可以解释一下吗?

最佳答案

尝试将 import issues.models 行移至 def read() 以防止过早加载模型。

关于python - Django - 无法运行 makemigrations : "no such table" even after running reset_db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42341965/

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