繁体   English   中英

Ruby on Rails:ratyrate gem表已经存在吗?

[英]Ruby on Rails: ratyrate gem table already exists?

我正在使用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

gem创建的迁移在更高版本的rails中不起作用。 在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是足够聪明,知道如何回滚此迁移。

暂无
暂无

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

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