简体   繁体   English

rails 迁移不适用于空的 function

[英]rails migration doesn't work with an empty down function

I generated a rails 3.2 migration with an empty down function, because the migration is irreversible (and I don't want to throw an exception).我生成了一个空的 function 的 Rails 3.2 迁移,因为迁移是不可逆的(我不想抛出异常)。 I run the migration successfully, but it has no effect.我成功运行了迁移,但没有任何效果。 When I rollback, and run db:migrate again, the effects does apply.当我回滚并再次运行 db:migrate 时,效果确实适用。

I solved this easily by filling the empty down function with a code which does nothing, but it's still pretty ugly.我通过用什么都不做的代码填充空的 function 轻松解决了这个问题,但它仍然很丑陋。

Does anyone knows why this happens?有谁知道为什么会这样? Is this a rails bug?这是轨道错误吗?

The exception is throwed to prevent destroying your database, if its irreversable, then that's probably the right thing to do.抛出异常是为了防止破坏你的数据库,如果它是不可逆的,那么这可能是正确的做法。 Your #down could look like this:你的#down看起来像这样:

  def down
    raise ActiveRecord::IrreversibleMigration, "Explain why its irreversable!"
  end

That will save others a lot of headache as it clearly notifies about irreversable migration and explains the reason behind it:)这将使其他人省去很多麻烦,因为它清楚地通知了不可逆转的迁移并解释了其背后的原因:)

EDIT: I cannot confirm this behaviour for Rails 3.2.3.编辑:我无法确认 Rails 3.2.3 的这种行为。 I've created several different migrations without #down , and no exceptino was raised.我在没有#down的情况下创建了几个不同的迁移,并且没有引发 exceptino 。 Maybe it's something in your code, which you didn't show a bit.也许这是您的代码中的某些内容,您没有显示一点。

EDIT 2: Just to recap, when you're using up/down method, its your responsibility to raise ActiveRecord::IrreversibleMigration .编辑 2:回顾一下,当您使用向上/向下方法时,您有责任提高ActiveRecord::IrreversibleMigration In other case, nothing will happen ( #down defined in AR will just return nil).在其他情况下,什么也不会发生(在 AR 中定义的#down将只返回 nil)。 The behaviour is different when you use #change .使用#change时行为不同。 In some cases the mentioned exception can be raised by #inverse defined here: https://github.com/rails/rails/blob/565bfb9cd49285ebaa170141b4996c22ba81de43/activerecord/lib/active_record/migration/command_recorder.rb#L39 which is expected behaviour.在某些情况下,可以通过此处定义的#inverse引发上述异常: https://github.com/rails/rails/blob/565bfb9cd49285ebaa170141b4996c22ba81de43/activerecord/lib/active_record/migration/command_recorder.rb#L39这是预期的行为。

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

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