ActiveRecord finding existing table indexes

后端 未结 4 908
逝去的感伤
逝去的感伤 2021-02-05 00:50

I am writing a migration generator for a plugin I am writing and I need to to be able to find what unique indexes a table has so I can modify existing unique indexes to become a

4条回答
  •  甜味超标
    2021-02-05 01:44

    This works with MySQL, SQLite3, and Postgres:

    ActiveRecord::Base.connection.tables.each do |table|
        puts ActiveRecord::Base.connection.indexes(table).inspect
    end
    

    But I think it only gives you the indexes you specifically created.

    Also, to find out which adapter is in use:

    ActiveRecord::Base.connection.class
    

提交回复
热议问题