如何在 Rails 中创建具有 NULLS LAST 的索引?

B S*_*ven 2 ruby-on-rails database-migration ruby-on-rails-4

我阅读了https://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_indexhttp://guides.rubyonrails.org/active_record_migrations.html

我不知道如何创建索引,如:

CREATE INDEX ON users (id DESC NULLS LAST);
Run Code Online (Sandbox Code Playgroud)

没有看到有关如何创建和索引的文档NULLS LAST

导轨 4.2.10

PostgreSQL

and*_*w21 5

我不相信 Rails 迁移支持索引选项NULLS LAST。您可能需要使用原始 SQL。

class ExampleMigration < ActiveRecord::Migration
  def up
    #add custom index
    execute <<-SQL
      CREATE INDEX index_users_on_id_nulls_last ON users (id DESC NULLS LAST);
    SQL
  end

  def down
    execute <<-SQL
      DROP INDEX index_users_on_id_nulls_last;
    SQL
  end
end
Run Code Online (Sandbox Code Playgroud)