如何修复损坏的 Rails 数据库迁移

发布于 2024-09-24 01:53:53 字数 470 浏览 2 评论 0原文

由于我自己的愚蠢,我设法以某种方式使我的数据库处于损坏状态,并且不知道如何修复它。

问题始于我的一个数据库迁移中的拼写错误。我正在添加一列,但拼写错误了要添加该列的表的名称。我运行了“rake db:migrate”但失败了。所以我去修正了拼写错误并再次运行“rake db:migrate”,但什么也没发生。因此,我认为也许 Rails 认为它​​已经完成了迁移,所以我运行了“rake db:rollback”,这似乎最终搞砸了事情,因为它删除了我试图添加列的表。

所有这一切的最终结果是我的数据库版本现在与rails认为的不同步。我尝试过使用“rake db:migrate:up”和“rake db:migrate:down”(带有版本号),但这没有帮助。即使当我尝试显式运行创建问题表的迁移时,也不会发生任何事情(如果我运行“向下”版本,它会告诉我它无法删除该表”)。我知道我可以直接访问数据库并修补问题表,但我认为最好找出这个问题是否有“正确”的解决方案。

非常感谢

As a direct result of my own stupidity I have managed to somehow get my database in a broken state and can't figure out how to fix it.

The problem started with a typo in one of my db migrations. I was adding a column and mis-spelled the name of the table that I wanted to add the column to. I ran 'rake db:migrate' and it failed. So I went and fixed the typo and ran 'rake db:migrate' again but nothing happened. So thinking that perhaps rails thought it had already done the migration, I ran 'rake db:rollback', which appeared to finally knacker things as it dropped the table I was trying to add the column to.

The end result of all this is that the version of my db is now out of sync with what rails thinks it is. I've tried using 'rake db:migrate:up' and 'rake db:migrate:down' (with version numbers) but this hasn't helped. Even when I try and explicitly run the migration that created the problem table, nothing happens (tho if I run the 'down' version it tells me it can't drop the table'). I know I could just access the db directly and patch up the problem table but I figured it would be good to find out if there is a 'proper' solution to this problem.

Many thanks

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

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

发布评论

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

评论(2

樱花细雨 2024-10-01 01:53:53

rake db:reset

出于某种原因,发布答案至少需要 30 个字符:-)

rake db:reset

and for some reason there's a 30-char minimum for posting an answer :-)

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