django 2.2 sqlite3 迁移到 mysql8 报错:return instance._state.fields_cache[cache_name] KeyError: 'art

2023-03-01 14:43:42 浏览数 (1)

错误日志

django 2.2(含 drf) sqlite3 转成 mysql5.7 ,执行 python manage.py loaddata data.json 时报错如下(有一个 article 模型)

代码语言:javascript复制
(joyoo) [root@VM_2_29_centos blog]# python manage.py loaddata data.json
/root/.virtualenvs/joyoo/lib/python3.6/site-packages/daphne/server.py:15: UserWarning: Something has already installed a non-asyncio Twisted reactor. Attempting to uninstall it; you can fix this warning by importing daphne.server early in your codebase or finding the package that imports Twisted and importing it later on.
  UserWarning,
System check identified some issues:
 
Traceback (most recent call last):
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 164, in __get__
    rel_obj = self.field.get_cached_value(instance)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/db/models/fields/mixins.py", line 13, in get_cached_value
    return instance._state.fields_cache[cache_name]
KeyError: 'article'
 
During handling of the above exception, another exception occurred:
 
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
    self.loaddata(fixture_labels)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 114, in loaddata
    self.load_label(fixture_label)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 181, in load_label
    obj.save(using=self.using)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/serializers/base.py", line 223, in save
    models.Model.save_base(self.object, using=using, raw=True, **kwargs)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/db/models/base.py", line 790, in save_base
    update_fields=update_fields, raw=raw, using=using,
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 175, in send
    for receiver in self._live_receivers(sender)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 175, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "/root/yzq/djangos/blog/blog/blog_signals.py", line 88, in change_comment
    comments = instance.article.comment_article_set.filter(is_delete=False).order_by(
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 178, in __get__
    rel_obj = self.get_object(instance)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 145, in get_object
    return qs.get(self.field.get_reverse_related_filter(instance))
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/db/models/query.py", line 408, in get
    self.model._meta.object_name
blog.models.DoesNotExist: Problem installing fixture '/root/yzq/djangos/blog/data.json': Article matching query does not exist.
Sentry is attempting to send 1 pending error messages
Waiting up to 10 seconds
Press Ctrl-C to quit

问题原因

是因为一个 APP 里面启动了 信号,迁移的时候回触发关联,导致报错

apps.py

代码语言:javascript复制
from django.apps import AppConfig


class BlogConfig(AppConfig):
    name = 'blog'
    verbose_name = "B 博客管理"

    # 启用信号,sqlite3 迁移到 mysql 要注释,不然会报错导致无法迁移
    def ready(self):
        import blog.blog_signals

解决办法

导入的时候注释掉信号,等导入完成再打开就行了。

导入日志

成功的导入日志(在台式机上导入到云服务器上,受带宽 1M 限制,130M 的 json 数据导入时间用了 差不多 4 小时)

代码语言:javascript复制
(joyo) E:git_storesblog>python manage.py loaddata data.json
E:py_envsjoyolibsite-packagesdaphneserver.py:15: UserWarning: Something has already installed a non-asyncio Twisted reactor. Attempting to uninstall it; you can fix this warning by importing daphne.server early in your codebase or finding the package that imports Twisted and importing it later on.
  UserWarning,
System check identified some issues:

WARNINGS:
finance.FundNet.fund: (fields.W342) Setting unique=True on a ForeignKey has the same effect as using a OneToOneField.
        HINT: ForeignKey(unique=True) is usually better served by a OneToOneField.
Building prefix dict from the default dictionary ...
jieba: 2023-02-28 16:25:29,733 E:py_envsjoyolibsite-packagesjieba__init__.py [line:111] DEBUG Building prefix dict from the default dictionary ...
Loading model from cache C:UsersyinzhAppDataLocalTempjieba.cache
jieba: 2023-02-28 16:25:29,736 E:py_envsjoyolibsite-packagesjieba__init__.py [line:131] DEBUG Loading model from cache C:UsersyinzhAppDataLocalTempjieba.cache
Loading model cost 0.957 seconds.
jieba: 2023-02-28 16:25:30,692 E:py_envsjoyolibsite-packagesjieba__init__.py [line:163] DEBUG Loading model cost 0.957 seconds.
Prefix dict has been built succesfully.
jieba: 2023-02-28 16:25:30,693 E:py_envsjoyolibsite-packagesjieba__init__.py [line:164] DEBUG Prefix dict has been built succesfully.
Installed 561564 object(s) from 1 fixture(s)

失败的导入日志(服务器内存不足,2H2G, 被系统干掉了)

代码语言:javascript复制
(joyoo) [root@VM_2_29_centos blog]# python manage.py loaddata /root/yzq/download/data.json
/root/.virtualenvs/joyoo/lib/python3.6/site-packages/daphne/server.py:15: UserWarning: Something has already installed a non-asyncio Twisted reactor. Attempting to uninstall it; you can fix this warning by importing daphne.server early in your codebase or finding the package that imports Twisted and importing it later on.
  UserWarning,
System check identified some issues:
 
WARNINGS:
finance.FundNet.fund: (fields.W342) Setting unique=True on a ForeignKey has the same effect as using a OneToOneField.
        HINT: ForeignKey(unique=True) is usually better served by a OneToOneField.
Building prefix dict from the default dictionary ...
jieba: 2022-04-12 01:35:45,402 /root/.virtualenvs/joyoo/lib/python3.6/site-packages/jieba/__init__.py [line:111] DEBUG Building prefix dict from the default dictionary ...
Loading model from cache /tmp/jieba.cache
jieba: 2022-04-12 01:35:45,406 /root/.virtualenvs/joyoo/lib/python3.6/site-packages/jieba/__init__.py [line:131] DEBUG Loading model from cache /tmp/jieba.cache
Loading model cost 1.018 seconds.
jieba: 2022-04-12 01:35:46,420 /root/.virtualenvs/joyoo/lib/python3.6/site-packages/jieba/__init__.py [line:163] DEBUG Loading model cost 1.018 seconds.
Prefix dict has been built succesfully.
jieba: 2022-04-12 01:35:46,434 /root/.virtualenvs/joyoo/lib/python3.6/site-packages/jieba/__init__.py [line:164] DEBUG Prefix dict has been built succesfully.
Killed

0 人点赞