【问题标题】:rake db:migrate doesnt work after paperclip gem is installedrake db:安装回形针 gem 后迁移不起作用
【发布时间】:2017-07-20 12:08:44
【问题描述】:

大家好,我在 Rails 上编写类似 Instagram 的应用程序,在我安装回形针 gem (5.1) 后,我创建了一个模型,我必须在数据库中迁移。正如我在标题中提到的 rake db:migrate 不起作用。我尝试按照一些解决方案的建议重新启动 rails 服务器和我的笔记本电脑,还尝试按照其他人的建议在 ActiveRecord::Migration 旁边添加 [5.1]。

class CreatePics < ActiveRecord::Migration[5.1]

改了一些版本后还是不行。 如果它有帮助,代码项目就在这里https://github.com/leobouts/Instagramm

我得到的错误是

rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

  class AddAttachmentImageToPics < ActiveRecord::Migration[4.2]
/Users/leonidas/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.2/lib/active_record/migration.rb:525:in `inherited'
/Users/leonidas/Desktop/Instagramm/db/migrate/20170719181859_add_attachment_image_to_pics.rb:1:in `<top (required)>'
/Users/leonidas/.rvm/gems/ruby-2.4.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'

这样继续下去

【问题讨论】:

  • 您应该在此处包含 AddAttachmentImageToPics 迁移
  • 我猜迁移类的版本号只添加在 Rails 5 中。当您使用 Rails 3 时,您应该有没有版本号的类名。尝试将其更改为 class CreatePics &lt; ActiveRecord::MigrationAddAttachmentImageToPics &lt; ActiveRecord::Migration[4.2]AddAttachmentImageToPics &lt; ActiveRecord::Migration
  • 我建议你重置你的数据库:rake db:reset
  • 感谢@Pavan 这正是问题所在,我对版本之间的差异感到有些困惑。我更改为最新的 rails 并使用迁移类的版本号。
  • 好吧,那我把它添加为答案。

标签: ruby-on-rails ruby rails-activerecord


【解决方案1】:

当您使用 Rails 3 时,迁移类不应具有version number。 AFAIK,迁移类的 version number 仅在 Rails 5.x 版本中添加。更改类应该可以修复错误。

class CreatePics < ActiveRecord::Migration

class AddAttachmentImageToPics < ActiveRecord::Migration

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 2011-01-07
    • 2012-07-04
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    相关资源
    最近更新 更多