简体   繁体   English

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

[英]How do I fix a broken rails db migration

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. 我运行了“ rake db:migrate”,但失败了。 So I went and fixed the typo and ran 'rake db:migrate' again but nothing happened. 所以我去改错了,再次运行“ rake db:migrate”,但是什么也没发生。 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. 因此,我以为Rails可能认为它已经完成了迁移,因此我运行了“ rake db:rollback”,当它删除了我试图向其添加列的表时,它似乎终于弄懂了。

The end result of all this is that the version of my db is now out of sync with what rails thinks it is. 所有这些的最终结果是我的数据库版本现在与Rails认为的不同步。 I've tried using 'rake db:migrate:up' and 'rake db:migrate:down' (with version numbers) but this hasn't helped. 我试过使用“ rake db:migrate:up”和“ rake db:migrate:down”(带有版本号),但这没有帮助。 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'). 即使当我尝试并明确地运行创建问题表的迁移时,也不会发生任何事情(如果我运行“ down”版本,它会告诉我它不能删除该表)。 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 非常感谢

我将在此处使用该解决方案: Rails:在早期开发阶段更改迁移

rake db:reset 耙db:重置

and for some reason there's a 30-char minimum for posting an answer :-) 出于某种原因,发布答案的最低要求为30个字符:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM