Will Gant
2006-Sep-11 16:06 UTC
How to add indexes and relationships in an ActiveRecord Migration
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
zero halo
2006-Sep-11 16:46 UTC
Re: How to add indexes and relationships in an ActiveRecord Migration
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Chris Hall
2006-Sep-11 17:10 UTC
Re: How to add indexes and relationships in an ActiveRecord Migration
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 On 9/11/06, Will Gant <williamwgant-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> 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. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---