新鲜 mysql 数据库正在获取 "mydb.background_task doesn't exist" - Django

Fresh mysql database getting "mydb.background_task doesn't exist" - Django

我全新安装了一个使用 background_tasks 的 django 应用程序。如果我删除使用 background_task 的模型,那么我就可以做到

python manage.py makemigrations

但是,如果我保留它们,我会得到一个 table 缺失异常:

"mydb.background_task doesn't exist"

我见过其他人遇到过这个问题,但我已经尝试了每一个建议: - 删除所有迁移文件 - 删除 sqlite3 转储文件 - 卸载 django 和 django-background-tasks - 确保我的机器即使在 virtualenv 之外也没有安装这两个包 - 我尝试删除我的 virtualenv 并重新安装所有内容。

我已经尝试了所有这些方法,python manage.py makemigrations 仍然给我这个例外。但除此之外别无他法。

Django==2.1.5
django-background-tasks==1.2.0
django-compat==1.0.15
django-mysql==3.2.0

p.s:我正在使用 Cloud SQL,我不知道这是否会影响任何事情,我不这么认为。 mysql

同样的问题

编辑: 添加跟踪

Traceback (most recent call last):
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/MySQLdb/cursors.py", line 198, in execute
    res = self._query(query)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/MySQLdb/cursors.py", line 304, in _query
    db.query(q)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/MySQLdb/connections.py", line 217, in query
    _mysql.connection.query(self, query)
MySQLdb._exceptions.ProgrammingError: (1146, "Table 'zynqdb_dev.background_task' doesn't exist")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/core/management/base.py", line 350, in execute
    self.check()
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/core/management/base.py", line 379, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/core/management/base.py", line 366, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/core/checks/registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/urls/resolvers.py", line 396, in check
    for pattern in self.url_patterns:
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/urls/resolvers.py", line 533, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Users/cottrell/tessel/venv/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/cottrell/tessel/tesselapp/tesselapp/urls.py", line 49, in <module>
    one_time_setup_background(priority=10)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/background_task/tasks.py", line 296, in __call__
    remove_existing_tasks)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/background_task/tasks.py", line 239, in schedule
    task.save()
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/background_task/models.py", line 315, in save
    return super(Task, self).save(*arg, **kw)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/models/base.py", line 718, in save
    force_update=force_update, update_fields=update_fields)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/models/base.py", line 748, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/models/base.py", line 831, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/models/base.py", line 869, in _do_insert
    using=using, raw=raw)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/models/query.py", line 1136, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1289, in execute_sql
    cursor.execute(sql, params)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/MySQLdb/cursors.py", line 198, in execute
    res = self._query(query)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/MySQLdb/cursors.py", line 304, in _query
    db.query(q)
  File "/Users/cottrell/tessel/venv/lib/python3.7/site-packages/MySQLdb/connections.py", line 217, in query
    _mysql.connection.query(self, query)
django.db.utils.ProgrammingError: (1146, "Table 'zynqdb_dev.background_task' doesn't exist")

我也试过了:

find . -path "*/migrations/*.pyc"  -delete
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete

我试过重新安装和卸载。

此外,如果我执行 python manage.py showmigrations.

也会出现同样的错误

这是模型的一部分,如果我注释掉@background,它就可以工作,但如果使用@background,它就不起作用。

def one_time_setup():
    if settings.SENTRY_TOKEN:
        sentry_sdk.init(
            settings.SENTRY_TOKEN,
            integrations=[DjangoIntegration()]
        )


#@background()
def one_time_setup_background():
    one_time_setup()


# Do one-time setup in django app, and background with high priority.
one_time_setup()
one_time_setup_background()

库中似乎有问题,请参阅 https://github.com/arteria/django-background-tasks/issues/204。您可能想暂时删除您的代码以迁移您的数据库。

尝试从 Documentation 中执行以下操作:

Install from PyPI:

pip install django-background-tasks

Add to INSTALLED_APPS inside your settings.py.

INSTALLED_APPS = (
# ...
'background_task',
# ...
)

Check that everything is fine with your migrations.

python manage.py showmigrations

Migrate your database and check if the background_task file is added to the migrations list.

python manage.py migrate

Make modifications in your views in which ever way you want to use the background_tasks. For example, you can look up a userID and send them message.

from background_task import background
from django.contrib.auth.models import User

@background(schedule=60)
def notify_user(user_id):
    # lookup user by id and send them a message
    user = User.objects.get(pk=user_id)
    user.email_user('Here is a notification', 'You have been notified')

听起来你没有删除每个 pycache 文件夹的内容,这意味着 Django 仍会尝试引用不存在的迁移。

这是我从 this blog post 中找到的用于清除迁移的易于使用的快捷方式。只需 运行 来自 CLI 在您的根项目文件夹 中的以下内容:

find . -path "*/migrations/*.py" -not -name "__init__.py" -not -path "./env/*" -delete
find . -path "*/migrations/*.pyc" -not -path "./env/*" -delete

之后,删除您的 sqlite 数据库,然后再次尝试迁移。

If you run this in the wrong directory, you will break your Django download, along with any 3rd party Django apps that use migrations!

我经常用到这个,所以我继续把它做成 bash function:

function djclear() {
  dir="${PWD##*/}"
  if [ -d $dir ]; then
       find . -path "*/migrations/*.py" -not -name "__init__.py" -not -path "./env/*" -delete
       find . -path "*/migrations/*.pyc" -not -path "./env/*" -delete
  else
    echo "Returning with exit code 1. Are you sure that you are in a project directory?"
    return 1
  fi
}

这看起来很复杂,但它真正做的是在执行命令之前确认您位于 Django 项目文件夹中。