Django 删除了 DDBB 表,不能再迁移:没有这样的表错误

Posted

技术标签:

【中文标题】Django 删除了 DDBB 表,不能再迁移:没有这样的表错误【英文标题】:Django deleted DDBB table and cannot migrate anymore: no such table error 【发布时间】:2020-06-03 23:49:40 【问题描述】:

我从 sqlite DDBB 中删除了一个表,因为我想进行一些更改,但无法通过简单更新 Django 中的模型来完成这些更改。从那以后,我无法再次执行迁移并为我的模型获取新表,出现错误“django.db.utils.OperationalError: no such table: shop_productitem”。

我尝试在其他类似线程中遵循许多提示,但没有运气。例如,我尝试过this 和this,但我无法解决问题。我的理解是,如果我运行 makemigrations 然后迁移模型,ddbb 应该使用新规范进行自我更新,但在这种特定情况下我可能弄错了。

谁能告诉我问题出在哪里?我担心我没有必要的知识来自己解决这个问题,我一直在寻找年龄。

请在下面找到我收到的整个错误消息:

    Applying shop.0002_auto_20200218_1958...Traceback (most recent call last):
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: shop_productitem

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 "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    fake_initial=fake_initial,
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/migration.py", line 122, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 133, in alter_field
    super().alter_field(model, old_field, new_field, strict=strict)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 509, in alter_field
    old_db_params, new_db_params, strict)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 330, in _alter_fi
eld
    self._remake_table(model, alter_field=(old_field, new_field))
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 260, in _remake_t
able
    disable_constraints=False,
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 95, in alter_db_t
able
    super().alter_db_table(model, old_db_table, new_db_table)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 383, in alter_db_tab
le
    "new_table": self.quote_name(new_db_table),
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 117, in execute
    cursor.execute(sql, params)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrapp
ers
    return executor(sql, params, many, context)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/EconopecoWebApp/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: shop_productitem

非常感谢, 歪斜

【问题讨论】:

【参考方案1】:

您是否尝试过从 migrations 文件夹中删除所有迁移(除 init 之外的所有迁移),然后运行 ​​ma​​kemigrationsmigrate

【讨论】:

迁移历史也保存在数据库中。所以仅仅删除文件是不行的。 嗨 Bruno,如果我这样做,迁移完成后最初没有任何错误,但如果我签入我的管理员并尝试打开 productitem,我会在 /admin/shop/productitem/ 处收到 OperationalError 没有这样的再次表:shop_productitem :( 嗨 weAreStarDust,你能给我一个提示吗?我实际上并没有尝试过这样做,并且可能是要走的路。非常感谢 @Askew 迁移历史存储在django_migrations 表中 我从来没有像 weAreStarDust 所说的那样做过,但是根据我现在在 Postgres 中看到的情况,有一个标有 django_migrations 的表,其中记录了每个完成的迁移。您可以尝试截断它。【参考方案2】:

如果您可以承受从 db 中丢失数据的后果,那么只需从其文件夹中删除旧的 db 文件即可。从迁移文件夹中删除所有迁移。然后运行 ​​makemigrationsmigrate 这将创建新的 sqllight DB

如果数据丢失不是一个选项,您可以尝试使用DB Browser for SQLite 或Command Line Shell For SQLite 手动重新创建已删除的表。然后使用迁移正确删除它们

以后,如果你需要删除表,你应该删除或注释它的模型代码,然后运行makemigrationsmigrate,这将正确地从数据库中删除表

【讨论】:

哦,是的,这就是我所做的,我做了 manage.py dbshel​​l,然后删除了我想删除的表,然后删除了旧的迁移并运行 make migrations 并再次迁移。一点运气都没有! @Askew 我建议了两个场景,有什么不清楚的地方问一下。 我通过命令行手动重新创建了表格,并按照您的指示进行操作,效果非常好。非常感谢我被困在这里多年!非常感谢! :) @Askew 很高兴听到,对数据库的每次操作进行备份是一个好习惯,这将为您节省大量时间:)

以上是关于Django 删除了 DDBB 表,不能再迁移:没有这样的表错误的主要内容,如果未能解决你的问题,请参考以下文章

Django 中的模型表被删除

django:删除表后怎么重新数据迁移生成表

Django 迁移损坏,不会忘记已删除的列

实体框架手动删除的表不能从EF迁移生成

django迁移地狱,丢了一张桌子。试图找回它

简单分析Flask 数据库迁移详情