【问题标题】:How to create an index that has NULLS LAST in Rails?如何在 Rails 中创建具有 NULLS LAST 的索引?
【发布时间】:2018-12-15 07:05:11
【问题描述】:

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

我看不到如何创建如下索引:

CREATE INDEX ON users (id DESC NULLS LAST);

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

Rails 4.2.10

PostgreSQL

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 database-migration


    【解决方案1】:

    我不相信 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
    

    【讨论】:

      猜你喜欢
      • 2018-01-24
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 2014-07-30
      • 2016-12-30
      • 2013-02-13
      相关资源
      最近更新 更多