gpt4 book ai didi

ruby-on-rails - Rails 3.1.0 迁移中remove_index 的正确语法是什么?

转载 作者:行者123 更新时间:2023-12-03 05:34:29 25 4
gpt4 key购买 nike

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

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

不会生成向下迁移,而且我在删除这些索引时遇到了很大的麻烦。我在文档中看到了不同的建议符号,在网上看到了不同的建议,但它们似乎都不适合我。例如...

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”就在那里......

我尝试过其他变体,包括

remove_index :users, :column => :email

remove_index :users, 'email'

或者:

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

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

让我失望的命令是:

bundle exec rake db:rollback STEP=1

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

最佳答案

郑重声明,按名称删除索引的方法是

remove_index(:table_name, :name => 'index_name')

所以在你的情况

remove_index(:users, :name => 'index_users_on_email')

关于ruby-on-rails - Rails 3.1.0 迁移中remove_index 的正确语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9028387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com