0

I'm in a Rails 3.2.3 app and I'm adding index for the foreign keys in my database migrations. However, when after running the migrations the keys don't seem to be indexed. I've tested directly via the mysql console. That's what I've done, better described:

I have a Car model that is related to an Owner model, like:

class Car
  belongs_to :owner
end

class Owner
  has_many :car
end

Since those were existing DB tables I created another migration to add the indexes, where I put:

add_index :cars, :owner_id, name: "owner_index"

Then I ran the migrations and according to Rails everything went fine. But when I log into my mysql, and run an EXPLAIN query, I don't see owner_index in the key collumn:

mysql> EXPLAIN SELECT * FROM cars WHERE owner_id = 41;
+-------------+------+---------------+---------+-------+---------+-------------+
| select_type | type | key           | key_len | ref   | rows    | Extra       |
+-------------+------+---------------+---------+-------+---------+-------------+
| SIMPLE      | ref  | NULL          | 5       | const |  108    | Using where | 
+-------------+------+---------------+---------+-------+---------+-------------+
1 row in set (0.00 sec)

Theoretically (and according to this post), the key column in the previous query should return owner_index and not NULL.

Have I done something wrong? What am I missing?

rodrigoalvesvieira
  • 7,895
  • 15
  • 63
  • 84

1 Answers1

0

Oh, I'm sorry, this is was just lack of attention of mine. The steps I described above work fine as long as you pass the name parameter to the add_index method.

rodrigoalvesvieira
  • 7,895
  • 15
  • 63
  • 84