2017-05-28 94 views
0

我使用的轨道5,我已经安装了宝石,并试图运行迁移,但我得到这个错误:Ruby on Rails:ratyrate gem表已经存在?

Index name 'index_rates_on_rater_id' on table 'rates' already exists

有谁知道为什么会存在?这是一个新的网站,刚刚开始添加设计宝石。

这是迁移文件,不会在执行完成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 
+0

如果您回滚并评论'add_index:rates,:rater_id' out并再次运行迁移,它是否有效? – Niklas

+0

你的db/schema.rb文件是什么样子的? – ddonche

回答

1

宝石创建迁移不会在轨的更高版本的工作。在Rails 5中,当您使用belongs_toreferences宏时,它们默认会创建索引和外键。

您真正需要的是这样的:

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 

你不需要updown因为Rails是足够聪明,知道如何回滚此迁移。

+0

我真的很怀疑这个特殊的宝石是否值得它根据github上的活动量。 – max

+0

这工作,谢谢!你认为我可以用什么替代这个宝石,@max – hellomello

+0

不知道,我猜这个功能并不是全部都很难单独复制。 – max