hel*_*llo 2 ruby ruby-on-rails rails-migrations ruby-on-rails-5
经过长时间的中断后我想回到RoR,当我试图rails db:migrate:
rails 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:
Run Code Online (Sandbox Code Playgroud)
错误继续......
我正在考虑它的原因gem ratyrate.
在其中一个迁移文件中:
class CreateRatingCaches < ActiveRecord::Migration
def self.up
create_table :rating_caches do |t|
t.belongs_to :cacheable, :polymorphic => true
t.float :avg, :null => false
t.integer :qty, :null => false
t.string :dimension
t.timestamps
end
add_index :rating_caches, [:cacheable_id, :cacheable_type]
end
def self.down
drop_table :rating_caches
end
end
Run Code Online (Sandbox Code Playgroud)
是因为导轨5不使用def self.up/ def self.down?而应该使用def change?
如果是这样的话,我可以更改def setf.up为def change然后删除def self.down块吗?
除此之外,为什么def self.down在创建表时甚至会调用表来删除表?只有在您db:rollback使用数据库时才会执行吗?
谢谢
由于您从ActiveRecord::Migration迁移中继承而被取消.
因此,您应该继承ActiveRecord::Migration[5.1]并且迁移应该起作用.将第一行更改为:
class CreateRatingCaches < ActiveRecord::Migration[5.1]
Run Code Online (Sandbox Code Playgroud)
(5.1指定导轨版本,相应调整,例如ActiveRecord::Migration[4.2]等)
有关迁移中的更改与上/下的更多信息,请参阅此答案,或者阅读官方指南中的此部分,以获取有关它们的不同方面的详细信息.