activerecord belongs to index

Hi folks, everything fine?

I'm about to create a patch for this, but before I prefer to ask you and listen to your opinions.

Shouldn't ActiveRecord::ConnectionAdapters::TableDefinition#belongs_to add indexes for the column by default? This is an approach that is almost always taken. And when the belongs_to is polymorphic is true, it should add indexes for both columns.

Thank you, waiting your replies.

I'm not sure it's that straight forward, it depends on how the
association is being used, is it the inverse of a has many, or just a
reference to some lookup table. if you're only ever accessing it
from the belongs_to side of the association then you're never doing a
query like

SELECT foo.whatever FROM foo WHERE bar_id = ?

In that case, the index isn't needed. So I don't think we need a
patch for this.