如何使用 django-south 恢复删除的表?

发布于 2024-10-22 17:44:55 字数 228 浏览 5 评论 0原文

我想清除数据库中的一个表,所以我删除了该表。通常我会执行manage.pysyncdb来重新创建它。然而,答案这里说不要不再使用syncdb。那么,我该怎么办呢?

I wanted to clear out a table in my DB, so I dropped the table. Normally I would do manage.py syncdb to re-create it. However, the answer here says not to use syncdb anymore. So, what do I do instead?

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

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

发布评论

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

评论(5

只是我以为 2024-10-29 17:44:56

有同样的问题。不确定这在所有情况下都有效,但这就是我所做的:

  1. 从 INSTALLED_APPS
  2. run manage.py syncdb
  3. 中取消注释“south”
  4. 中注释掉“south”,在 INSTALLED_APPS run manage.py migrate

瞧!

您的里程可能会有所不同......

Had the identical problem. Not sure this works in all circumstances, but here's what I did:

  1. comment out "south" from INSTALLED_APPS
  2. run manage.py syncdb
  3. uncomment "south" in INSTALLED_APPS
  4. run manage.py migrate

Voila!

Your mileage may vary....

幸福丶如此 2024-10-29 17:44:56

嗯,这次交流涵盖了我的问题:

如果您手动修改数据库,South 不会注意到 - 这是唯一的方法
跟踪数据库的版本
South_migrationhistory 表,所以如果你在它背后摆弄,它是
你有责任修复它。

我最终做的是注释掉我删除的有问题的模型,进行schemamigration,创建一个我删除的空无列表(这样 South 就可以删除一些东西),迁移,然后取消注释模型,schemamigration并再次迁移。比仅仅删除表和syncdb更烦人,但是啊好吧。

Hmm this exchange covers my very question:

If you modify the database by hand, South won't notice - its only way of
keeping track of what version the database is is the
south_migrationhistory table, so if you fiddle behind its back, it's
your responsibility to fix it.

What I ended up doing was commenting out the model that I dropped in question, doing a schemamigration, creating an empty no-column table of the one I dropped (so South has something to drop), migrateing, then un-commenting the model, schemamigration and migrateing again. A bit more annoying than just dropping the table and syncdb but ah well.

夏花。依旧 2024-10-29 17:44:56

确保您的所有迁移均已应用:
python manage.py migrate

告诉 Django 创建模型中的表:python manage.pysyncdb

告诉 South 一切都在它应该在的地方:python manage.py migrate appname --fake

这假设自您创建上次迁移以来,任何模型都没有发生任何变化。

Make sure all your migrations are applied:
python manage.py migrate

Tell Django to create the tables as they are in your models: python manage.py syncdb

Tell South that everything is where it should be: python manage.py migrate appname --fake

This assumes that nothing has changed in any of your models since you created your last migration.

A君 2024-10-29 17:44:56

我知道这个问题很旧,但我刚刚遇到这个问题,并认为我会发布我的解决方案,以防这对任何人有帮助。

  1. 进入数据库所在的 models.py 文件夹。
  2. models.py 文件中剪切整个类。
  3. 运行 ./manage.py schemamigration appname --auto (这将创建另一个迁移,其中 South 将识别并删除此表)。您可能需要在数据库中重新创建一个空白表,以便 South 可以看到它。
  4. 运行迁移,该表应该从数据库中删除。
  5. 将表类重新粘贴回其在 models.py 文件中的位置。
  6. 运行 ./manage.py schemamigration appname --auto。 South 应该选择该表并允许您迁移
  7. Run ./manage.py migrate appname 并且 South 应该将该表重新添加回您的数据库...以及列等,但没有数据,显然。 :)

I know this issue is old, but I just ran into this issue and thought I'd post my solution in case this helps anyone.

  1. Go into your models.py folder where the database is.
  2. Cut the entire class out of the models.py file.
  3. Run ./manage.py schemamigration appname --auto (this will create another migration where South will recognize to delete this table). You may need to recreate a blank table in you database so South sees it.
  4. Run the migration and the table should drop from your database.
  5. Re-paste in your table class back to where it was in your models.py file.
  6. Run a ./manage.py schemamigration appname --auto. South should pick up the table and allow you to migrate
  7. Run ./manage.py migrate appname and South should re-add the table back into your databasse... with the columns and such again, but without the data, obviously. :)
三寸金莲 2024-10-29 17:44:55

这是一个相当晚的回应,但对于那些会遇到同样问题的人(就像我一样)。

通常要删除由 South 管理的应用程序的 db_tables,您应该使用:

python manage.py migrate appname zero

但是如果您在数据库中手动删除它们,请让 South 了解它

python manage.py migrate appname zero --fake

当然要重新创建表

python manage.py migrate appname

It's a pretty late response but for people who will run into the same issue (like I did).

Normally to drop the db_tables for the app that is managed by south you should use:

python manage.py migrate appname zero

But if you dropped them manually in the db let south know about it

python manage.py migrate appname zero --fake

And of course to recreate the tables

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