Django - 不能运行makemigrations:"没有这样的表"甚至在运行reset_db之后

时间:2017-02-20 10:22:21

标签: python django

如果我的INSTALLED_APPS中有某个应用,则无法运行makemigrationsmigrate或其他任何内容(flushreset_db来自django-extensions)。

该应用名为issues,有一个型号:

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

它之前正在运行(makemigrations和migrate运行正常,我可以正确使用app / model),直到我尝试添加:

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时会出现同样的错误。

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

我已经尝试过上面的操作然后运行makemigrations并进行迁移,这也行不通。一旦我把问题放回到INSTALLED_APPS中,一切都会被破坏。

我该如何解决这个问题?

编辑:

也许相关,问题模型有一个具有功能的管理器:

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() ;with x as ( select idx, row_number() over(partition by Column2 order by Column1) as new_idx from tbl ) update x set idx = new_idx 下方显示的行来解决问题:

 var yourDataStr = JSON.stringify(yourdata) 

以某种方式导致事情破裂,我不明白为什么。有人可以解释一下吗?

1 个答案:

答案 0 :(得分:2)

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