gpt4 book ai didi

更改应用程序名称和数据库表后,Django 无法进行迁移

转载 作者:行者123 更新时间:2023-12-02 20:33:02 25 4
gpt4 key购买 nike

我正在使用 Django 1.11.5 并使用 PyCharm 作为我的 IDE。我一直在尝试将我的应用程序名称从“clinicaltrials”重构为“cancer_trials”。 PyCharm 相应地更新了我的所有项目文件。然后我按照 this SO answer 中的步骤操作更新适当的数据库表。但是,当我尝试运行 makemigration 时,出现以下错误。我似乎无法弄清楚这意味着什么和/或我在这里缺少什么部分。

    > python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Python_3.6.1\lib\site-packages\django\core\management\__init__.py", line 364, in execute_from_command_line
utility.execute()
File "C:\Python_3.6.1\lib\site-packages\django\core\management\__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python_3.6.1\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python_3.6.1\lib\site-packages\django\core\management\base.py", line 330, in execute
output = self.handle(*args, **options)
File "C:\Python_3.6.1\lib\site-packages\django\core\management\commands\makemigrations.py", line 150, in handle
loader.project_state(),
File "C:\Python_3.6.1\lib\site-packages\django\db\migrations\loader.py", line 323, in project_state
return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps))
File "C:\Python_3.6.1\lib\site-packages\django\db\migrations\graph.py", line 409, in make_state
project_state = self.nodes[node].mutate_state(project_state, preserve=False)
File "C:\Python_3.6.1\lib\site-packages\django\db\migrations\migration.py", line 92, in mutate_state
operation.state_forwards(self.app_label, new_state)
File "C:\Python_3.6.1\lib\site-packages\django\db\migrations\operations\fields.py", line 142, in state_forwards
for name, instance in state.models[app_label, self.model_name_lower].fields:
KeyError: ('cancer_trials', 'cancer_trials')

这是抛出错误的函数

def state_forwards(self, app_label, state):
new_fields = []
old_field = None
for name, instance in state.models[app_label, self.model_name_lower].fields:
if name != self.name:
new_fields.append((name, instance))
else:
old_field = instance
state.models[app_label, self.model_name_lower].fields = new_fields
# Delay rendering of relationships if it's not a relational field
delay = not old_field.is_relation
state.reload_model(app_label, self.model_name_lower, delay=delay)

最佳答案

根据我的经验,最简单的解决方案是创建新应用程序并复制代码:

  1. 使用所需名称创建新应用并将其添加到设置
  2. 将代码从旧应用复制/粘贴到新应用,将引用从旧应用更改为新应用,运行 makemigrations 并迁移
  3. 打开数据库并将数据从旧表复制到新表
  4. 检查新应用中的一切是否正常
  5. 搜索 stackoverflow.com 或 google 如何从项目中删除应用程序或直接保留在那里。不幸的是,我对这些步骤并不是 100% 肯定,如果我错了,请有人纠正我,但据我记忆:

关于更改应用程序名称和数据库表后,Django 无法进行迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48080717/

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