Ruby-on-rails Rails rake db:migrate 没有效果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5598221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:53:15  来源:igfitidea点击:

Rails rake db:migrate has no effect

ruby-on-railsruby-on-rails-3activerecordrakerails-migrations

提问by Hyman Hoge

I made a new Rails 3 app today, added a simple migration, and for some reason, nothing happens when I do rake db:migrate. It simply pauses a few seconds, then returns to the command prompt, with no errors or anything. Schema.rb and the database stay empty.

我今天做了一个新的 Rails 3 应用程序,添加了一个简单的迁移,由于某种原因,当我执行 rake db:migrate 时没有任何反应。它只是暂停几秒钟,然后返回到命令提示符,没有错误或任何东西。Schema.rb 和数据库保持为空。

Any ideas what could be going on? I've made many apps and never had this problem. Everything is a totally standard setup too.

任何想法可能会发生什么?我制作了很多应用程序,但从未遇到过这个问题。一切都是完全标准的设置。

回答by tadman

There's a few reasons why your migrations won't run, but the most common is that the system is already under the impression that all the migrations you've defined have already run.

迁移无法运行的原因有多种,但最常见的原因是系统已经认为您定义的所有迁移都已运行。

Each migration creates an entry in the schema_migrationstable with the versioncolumn corresponding to the identifier number. If you want to force a migration to re-run you can usually back it out and retry it. For example, if you had 20100421175455_create_things.rbthen you would re-run it using:

每个迁移会在一个条目schema_migrations表与version对应于该标识符数列。如果您想强制重新运行迁移,您通常可以将其退出并重试。例如,如果您有,20100421175455_create_things.rb那么您将使用以下命令重新运行它:

rake db:migrate:redo VERSION=20100421175455

A common situation is that your migration has failed to run in the first place, that it generated an exception for instance, and yet Rails still considers it complete. To forcibly re-run a migration, delete the corresponding record from the schema_migrationstable and run rake db:migrateagain.

一种常见的情况是您的迁移首先未能运行,例如它生成了一个异常,但 Rails 仍然认为它已完成。要强制重新运行迁移,请从schema_migrations表中删除相应的记录并rake db:migrate再次运行。

One way to avoid this kind of problem in the future is to define your migrations with an automatic back-out procedure:

将来避免此类问题的一种方法是使用自动退出程序定义迁移:

class CreateThings < ActiveRecord::Migration
  def self.up
    # ... (migration) ...

  rescue
    # If an exception occurs, back out of this migration, but ignore any
    # exceptions generated there. Do the best you can.
    self.down rescue nil

    # Re-raise this exception for diagnostic purposes.
    raise
  end
end

If you have a mistake in your migration you will see the exception listed on the console. Since the migration has automatically been rolled back you should be able to run it again and again until you get it right.

如果您在迁移过程中出现错误,您将在控制台上看到列出的异常。由于迁移已自动回滚,因此您应该能够一次又一次地运行它,直到正确为止。

回答by limlim

Calling spring stopmight solve your problems.

打电话spring stop可能会解决您的问题。

回答by Hyman Hoge

Well, I found out what was causing my problem. I'm using the slim_scrooge gem and commenting it out makes everything proceed normally. Don't know why though...

好吧,我发现是什么导致了我的问题。我正在使用 slim_scrooge gem 并将其注释掉会使一切正常进行。虽然不知道为什么...

回答by Gaurav

I faced the same problem. I did a kind of a short hack that helped me. I am posting it just in case anyone wants a short and sweet solution. I agree with what Tadman is saying

我遇到了同样的问题。我做了一种对我有帮助的简短黑客。我发布它以防万一有人想要一个简短而甜蜜的解决方案。我同意 Tadman 所说的

"the system is already under the impression that all the migrations you've defined have already run"

“系统已经认为您定义的所有迁移都已经运行”

What I did was to change the name of the migrate file in the /app_folder/db/migratefolder. I think the numeric part in the name of the ruby migrate file is the time at which the file was created.

我所做的是更改/app_folder/db/migrate文件夹中迁移文件的名称。我认为 ruby​​ 迁移文件名称中的数字部分是创建文件的时间。

You can add say 1 , to the filename, every time you want to re-run the migrate. After changing the name drop/delete the table (I used mysql command line tool for deleting) and then run rake db:migrateand the migrations should be done.

每次要重新运行迁移时,您都可以在文件名中添加 say 1 。更改名称后删除/删除表(我使用 mysql 命令行工具进行删除),然后运行rake db:migrate并完成迁移。

回答by Anton R

I faced similar problem today while migrating plugin for Redmine using

我今天在使用 Redmine 迁移插件时遇到了类似的问题

rake redmine:plugins:migrate RAILS_ENV=production NAME=plugin_name

where plugin_name is actually plugin name defined in init.rb of the plugin.

其中 plugin_name 实际上是在插件的 init.rb 中定义的插件名称。

I struggled for 4 hours and finally figured out that my plugin directory name was not the same as the plugin name (note redmine_prefix):

折腾了4个小时终于发现我的插件目录名和插件名不一样(注意redmine_前缀):

~/redmine/plugins/redmine_plugin_name

So, make sure your plugin is placed in folder named after plugin name. I believe it applies to other rails apps as well.

因此,请确保您的插件放置在以插件名称命名的文件夹中。我相信它也适用于其他 rails 应用程序。