ActiveRecord的发现现有的表索引 [英] ActiveRecord finding existing table indexes

查看:79
本文介绍了ActiveRecord的发现现有的表索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个插件,我写一个迁移发生器,我需要能够找到唯一索引表中有这样我就可以修改现有的唯一索引,以成为一个复合唯一索引。我一直试图找到一种方法来访问的内容索引的表与ActiveRecord的。我只能够找到的ActiveRecord :: ConnectionAdapters :: PostgreSQLAdapter ::索引方法,但不幸的是,这是仅适用于PosgreSQLAdapter。我需要能够支持其他主要的数据库。

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 composite unique index. I have been trying to find a way to access what indexes a table has with ActiveRecord. I have only been able to find ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::indexes method, but unfortunately this is only available for the PosgreSQLAdapter. I need to be able to support the other major databases.

我第一次grepping的schema.rb文件,以查找索引,这个工作在第一,但我很快就意识到这是一个糟糕的策略。

I first grepping the schema.rb file to find the indexes, this worked at first, but I soon realized this was a bad strategy.

我在想,如果ActiveRecord的不提供,能够做到这一点的多个数据库适配器的手段,我可​​能可以写适配器特定查询检索表的索引信息。如果我需要采取这种方法,这将是一个很好的方法,以确定所使用的适配器?

I was thinking that if ActiveRecord does not provide a means to be able to do this for multiple database adapters, I may be able to write adapter specific queries to retrieve the index information from a table. If I need to resort to this method what would be a good way to determine the adapter in use?

如果有人知道获得的ActiveRecord列出表索引的信息,这将是理想的。

If someone knows of a way to get ActiveRecord to list table index information that would be ideal.

推荐答案

这工作与MySQL,SQLite3的和Postgres的:

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

这篇关于ActiveRecord的发现现有的表索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆