Django 测试无法运行迁移

发布于 2025-01-15 15:44:09 字数 2249 浏览 2 评论 0原文

我正在尝试运行一个测试脚本,遵循 this django doc< /a> (这是此处使用的版本)。如果堆栈很长,它很快就会失败。我已经选择了可能的罪魁祸首。

  File "/home/user11/app-master/en/lib/python3.8/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/home/user11/app-master/app/colegiados/migrations/0002_auto_20200128_1646.py", line 185, in migrate
    add_sistema_entidade_e_orgao_composicao(apps, sistema)
  File "/home/user11/app-master/app/colegiados/migrations/0002_auto_20200128_1646.py", line 16, in add_sistema_entidade_e_orgao_composicao
    user = get_user(apps)
  File "/home/user11/app-master/app/colegiados/migrations/0002_auto_20200128_1646.py", line 7, in get_user
    return User.objects.filter(
  File "/home/user11/app-master/en/lib/python3.8/site-packages/django/db/models/query.py", line 318, in __getitem__
    return qs._result_cache[0]
IndexError: list index out of range

作为解决方法,我修改了 django 的 query.py

if qs._result_cache:
  return qs._result_cache[0]
else: 
  return ""

,它有效,直到出现下一个错误:

  File "/home/user11/app-master/en/lib/python3.8/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/home/user11/app-master/app/core/migrations/0016_auto_20201120_0934.py", line 106, in migrate
    sistema = Sistema.objects.get(nom_alias=sistema)
  File "/home/user11/app-master/en/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/user11/app-master/en/lib/python3.8/site-packages/django/db/models/query.py", line 439, in get
    raise self.model.DoesNotExist(
__fake__.DoesNotExist: Sistema matching query does not exist.

现在我被卡住了。 test_database 是使用所有表创建的,直至这些迁移错误,其中绝大多数缺乏任何数据。在那些空的表中,有最后一个错误似乎引用的表。

请注意,我不是开发人员,我无权创建正在使用的数据库或任何迁移。我强烈怀疑数据库(Postgres12)必须从“最小”备份创建/恢复,然后迁移才能正常工作。这可能是这些失败的原因吗?如果是这样,我可以选择哪些选项来运行不更改已部署数据库的 django 测试?是否有任何选项可以将测试作为查询块运行,然后进行回滚,因为它使用的是 Postgres?

I'm trying to run a test script, following this django doc (which is the version being used here). It quickly fails with a long stack. I've selected what's the possible culprit.

  File "/home/user11/app-master/en/lib/python3.8/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/home/user11/app-master/app/colegiados/migrations/0002_auto_20200128_1646.py", line 185, in migrate
    add_sistema_entidade_e_orgao_composicao(apps, sistema)
  File "/home/user11/app-master/app/colegiados/migrations/0002_auto_20200128_1646.py", line 16, in add_sistema_entidade_e_orgao_composicao
    user = get_user(apps)
  File "/home/user11/app-master/app/colegiados/migrations/0002_auto_20200128_1646.py", line 7, in get_user
    return User.objects.filter(
  File "/home/user11/app-master/en/lib/python3.8/site-packages/django/db/models/query.py", line 318, in __getitem__
    return qs._result_cache[0]
IndexError: list index out of range

As a workaround, I modified django's query.py

if qs._result_cache:
  return qs._result_cache[0]
else: 
  return ""

Which worked, until the next error:

  File "/home/user11/app-master/en/lib/python3.8/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/home/user11/app-master/app/core/migrations/0016_auto_20201120_0934.py", line 106, in migrate
    sistema = Sistema.objects.get(nom_alias=sistema)
  File "/home/user11/app-master/en/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/user11/app-master/en/lib/python3.8/site-packages/django/db/models/query.py", line 439, in get
    raise self.model.DoesNotExist(
__fake__.DoesNotExist: Sistema matching query does not exist.

Now I'm stuck. The test_database gets created with all the tables up to these migrations' errors, with the vast majority lacking any data. Among those that are empty is the table that this last error seems to refer to.

Note that I'm not the developer, I had no hand in creating the DB being used nor any of the migrations. I strongly suspect the database (Postgres12) has to be created/restored from a "minimal" backup before migrations can work properly. Could that be the reason for these failures? If so, what are my options for running a django test that doesn't alter the deployed database? Any options for running the test as a query block and then doing a rollback, as it's using Postgres?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后来的我们 2025-01-22 15:44:09

在与团队其他成员进行一些交谈后,决定重置所有迁移以确保此类问题不再发生。也许不是“预期”的解决方案,但却是一个不错的解决方案。

After some talk to the rest of the team, it was decided to reset all migrations to ensure this type of problem no longer happens. Maybe not the "expected" solution, but a decent solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文