class UpdateIndexOnUsers < ActiveRecord::Migration
def up
sql = 'DROP INDEX index_users_on_email'
sql << ' ON users' if Rails.env == 'production' # Heroku pg
ActiveRecord::Base.connection.execute(sql)
end
end
在heroku上尝试rake db:migrate时出现语法错误。任何帮助将不胜感激。使用Postgresql。我应该提一下它在当地运作良好。
编辑:错误
PG::SyntaxError: Error: syntax error at or near "ON"
Line 1: DROP INDEX index_users_on_email ON users
^
答案 0 :(得分:1)
最简单的方法是使用rails的帮助程序,让它们处理数据库的具体细节:
class UpdateIndexOnUsers < ActiveRecord::Migration
def up
remove_index :users, :email
end
end
如果最初创建的索引名称不是rails默认使用的名称,那么
class UpdateIndexOnUsers < ActiveRecord::Migration
def up
remove_index :users, :email, :name => 'name_of_index'
end
end
答案 1 :(得分:0)
据我所知,Postgresql在删除索引时不支持(或需要)ON xxxx
。文档也没有提到它:
http://www.postgresql.org/docs/9.2/static/sql-dropindex.html
只需指定索引名称即可,但您可以使用架构名称对其进行限定,如果您有标准设置,则可能不需要这样做。
如果您还在本地使用postgresql,那么可能它正在运行,因为ON users
仅在rails env为production
时添加,而您可能会在development
中运行{1}}本地。