hel*_*llo 0 ruby-on-rails rails-migrations ruby-on-rails-5
我正在使用Rails 5,并且已经安装了gem并尝试运行迁移,但是却出现此错误:
Index name 'index_rates_on_rater_id' on table 'rates' already exists
有人知道为什么会这样吗?这是一个新站点,并且刚刚开始添加devise gem。
这是在执行时无法完成的迁移文件 rails db:migrate
class CreateRates < ActiveRecord::Migration[5.1]
def self.up
create_table :rates do |t|
t.belongs_to :rater
t.belongs_to :rateable, :polymorphic => true
t.float :stars, :null => false
t.string :dimension
t.timestamps
end
add_index :rates, :rater_id
add_index :rates, [:rateable_id, :rateable_type]
end
def self.down
drop_table :rates
end
end
Run Code Online (Sandbox Code Playgroud)
gem创建的迁移在更高版本的rails中不起作用。在Rails 5中,当您使用belongs_to和references宏时,它们会默认创建索引和外键。
您真正需要的是:
class CreateRates < ActiveRecord::Migration[5.1]
def self.change
create_table :rates do |t|
t.belongs_to :rater
t.belongs_to :rateable, polymorphic: true
t.float :stars, null: false
t.string :dimension
t.timestamps
end
add_index :rates, [:rateable_id, :rateable_type]
end
end
Run Code Online (Sandbox Code Playgroud)
您不需要,up而且down由于Rails足够聪明,可以知道如何回滚此迁移。
| 归档时间: |
|
| 查看次数: |
671 次 |
| 最近记录: |