Ben
Ben

Reputation: 759

ActiveRecord default indexes

Does Rails (v3) create an index on the id column by default, or do I have to add the line

"add_index :table, :id, :unique => true"

into the migration file?

Upvotes: 4

Views: 1681

Answers (1)

coreyward
coreyward

Reputation: 80041

ActiveRecord requires that every table has a primary key. It is called "id" by default. You don't need to add it in your migrations.

If you are creating a reference to another table, you will need to create the reference columns somewhat manually. You can either do t.integer :user_id or (my preference) t.belongs_to :user. The latter is slower, though, because Rails will invoke ActiveRecord to determine what to call the user reference column.

Upvotes: 4

Related Questions