How to add indexes and relationships in an ActiveRecord Migr

Hello all,
Is there a way to add relationships and table indexes in an active
record migration. I’m less concerned about the indexes, but the lack of
relationships seems to be a real problem. Any thoughts?


New Yahoo! Messenger with Voice. Call regular phones from your PC and
save big.

Use the execute command (outside the create table loop) to inject SQL
statements for whatever isn’t covered by the migration syntax (which is
limited). For example:

execute “ALTER TABLE items ADD COLUMN cost_unit DECIMAL(5,2) NOT NULL
DEFAULT 0”

Also check out the foreign_key_migrations plugin.
(http://www.redhillconsulting.com.au/rails_plugins.html). I use it to
handle relationships automatically.

indexes are simple

add_index :table_name, :column_name

or for multiple columns

add_index :table_name, [:column_name1, :column_name2, …]

and to remove an index

remove_index :table_name, :column_name

in the case of a multi-column index, rails just uses the first column
name to generate the index name, so in order to remove it, just
provide the first column in the index

for creating foreign key relationships, it is also quite simple but
there are no built-in rails methods for creating them. in self.up,
after your create_table call, add

execute “alter table things add constraint fk_things_widget_id foreign
key (widget_id) references widgets(id)”

and in self, down, before the drop_table call, add

execute “alter table things drop foreign key fk_things_widget_id”

Chris