Rails 3.1.0 迁移中 remove_index 的正确语法是什么? [英] What's the correct syntax for remove_index in a Rails 3.1.0 migration?

查看:26
本文介绍了Rails 3.1.0 迁移中 remove_index 的正确语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Devise 添加到现有的 Rails 应用程序中,并且已经定义了一个用户表.设计生成器推出了以下迁移:

I'm in the process of adding Devise to an existing Rails app, with a Users table already defined. The devise generator pushed out the following migration:

class AddDeviseToUsers < ActiveRecord::Migration
  def self.up
    change_table(:users) do |t|

     ## Database authenticatable
     t.string :email,              :null => false, :default => ""
     t.string :encrypted_password, :null => false, :default => ""

     ## Recoverable
     t.string   :reset_password_token
     t.datetime :reset_password_sent_at

     ## Rememberable
     t.datetime :remember_created_at

     ## Trackable
     t.integer  :sign_in_count, :default => 0

     blah blah blah....

   end

   add_index :users, :email,                :unique => true
   add_index :users, :reset_password_token, :unique => true
 end

未生成向下迁移,我正在删除这些索引.我在文档中看到不同的建议符号,以及不同的在线建议,但似乎没有一个对我有用.例如...

The downward migration isn't generated, and I'm having a heck of a time removing those indexes. I'm seeing different suggested notation in the documentation, and different suggestions online, but none of them seem to be working for me. For example...

def self.down
  change_table(:users) do |t|
    t.remove  :email
    t.remove  :encrypted_password

    t.remove  :reset_password_token

    blah blah blah...
  end

  remove_index :users, :email
  remove_index :users, :reset_password_token
end

结果...

An error has occurred, this and all later migrations canceled:

Index name 'index_users_on_email' on table 'users' does not exist

这很奇怪,因为如果我检查数据库,果然,'index_users_on_email' 就在那里......

which is odd, because if I check the database, sure enough, 'index_users_on_email' is right there...

我尝试了其他变体,包括

I've tried other variations, including

remove_index :users, :column => :email

remove_index :users, 'email'

或:

change_table(:users) do |t|
  t.remove_index :email
end

...但没有骰子.我正在使用 Postgres 运行 Rails 3.1.0、Ruby 1.9.2、rake 0.9.2.2.

...but no dice. I'm running Rails 3.1.0, Ruby 1.9.2, rake 0.9.2.2, with Postgres.

让我失望的命令是:

bundle exec rake db:rollback STEP=1

成功应用迁移后.有什么建议吗?

after successfully apply the migration up. Any advice?

推荐答案

根据数据库类型,您无需担心删除 self.down 方法中的索引,因为索引删除列时会自动从数据库中删除.

Depending on the database type, you don't need to worry about removing the indexes in the self.down method since the index will automatically be removed from the database when you drop the column.

你也可以在你的 self.down 方法中使用这个语法:

You can also use this syntax in your self.down method:

def self.down
   remove_column :users, :email
   remove_column :users, :encrypted_password
   remove_column :users, :reset_password_token
end

这篇关于Rails 3.1.0 迁移中 remove_index 的正确语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆