Hello, I created a table and about 10 migrations down the line I decided that I needed to add an ID column (this was actually a table for a has_many :through and I recently realized that it needs an ID column, unlike HABTM tables). I am a big perfectionist and I frequently run "SHOW COLUMNS IN table;", so I naturally expect to see the "id" column the first one listed. However, if I just do a standard add_column() migration, the column will appear on the bottom. Is there any way to re-arrange this? The Rails docs says that there is an options parameter for add_column, but I couldn''t really find much information on it. I could, of course, manually write the SQL, but I want to see if there is a way to do this built-in to add_column(). The syntax would be: ADD [COLUMN] column_definition [FIRST | AFTER col_name ] So I tried: add_column :memberships, :id, :integer, :first => true There was no error, but it did not put it first. Is it possible to do this? I know it doesn''t really matter, but it bothers me...Thanks! Michael
Michael Schreifels wrote:> Hello, > > I created a table and about 10 migrations down the line I decided that > I needed to add an ID column (this was actually a table for a has_many > :through and I recently realized that it needs an ID column, unlike > HABTM tables). I am a big perfectionist and I frequently run "SHOW > COLUMNS IN table;", so I naturally expect to see the "id" column the > first one listed. However, if I just do a standard add_column() > migration, the column will appear on the bottom. Is there any way to > re-arrange this? The Rails docs says that there is an options > parameter for add_column, but I couldn''t really find much information > on it. I could, of course, manually write the SQL, but I want to see > if there is a way to do this built-in to add_column().> [snip]I believe this is database dependent. Oracle, for instance, provides no way to do this other than to drop and recreate the table with the columns in the order you prefer. Ofcourse, when you need an ID column, you''re pretty much bound to be doing some SQL hackery anyway, as that will be a required (primary key) column by definition, and if your database table contains any data whatsoever.. You know :) Regards, Henning Kilset
On 7/18/06, Henning Kilset <henning@railsfoundry.com> wrote:> Michael Schreifels wrote: > > Hello, > > > > I created a table and about 10 migrations down the line I decided that > > I needed to add an ID column (this was actually a table for a has_many > > :through and I recently realized that it needs an ID column, unlike > > HABTM tables). I am a big perfectionist and I frequently run "SHOW > > COLUMNS IN table;", so I naturally expect to see the "id" column the > > first one listed. However, if I just do a standard add_column() > > migration, the column will appear on the bottom. Is there any way to > > re-arrange this? The Rails docs says that there is an options > > parameter for add_column, but I couldn''t really find much information > > on it. I could, of course, manually write the SQL, but I want to see > > if there is a way to do this built-in to add_column(). > > > [snip] > I believe this is database dependent. Oracle, for instance, provides no > way to do this other than to drop and recreate the table with the > columns in the order you prefer. Ofcourse, when you need an ID column, > you''re pretty much bound to be doing some SQL hackery anyway, as that > will be a required (primary key) column by definition, and if your > database table contains any data whatsoever.. You know :) > > Regards, > Henning KilsetAs Henning points out it''s totally dependant but you could always use a specific adaptor within a migration and just manually run the query. This would make the migration less useful if you decide to start using another db type but in the short term it would work. If this is a concern about the future you could adjust your databases manually for now and add the column in the original migration file (10 migrations ago) and your future migrations will work nicely. The source is pretty specific about what options it takes and order isn''t one of them. Actually, it''s the first time I''ve seen the docs really, really groove with the source so perfectly. Cheers, Chuck Vose
On 7/18/06, Chuck Vose <vosechu@create-on.com> wrote:> On 7/18/06, Henning Kilset <henning@railsfoundry.com> wrote: > > Michael Schreifels wrote: > > > Hello, > > > > > > I created a table and about 10 migrations down the line I decided that > > > I needed to add an ID column (this was actually a table for a has_many > > > :through and I recently realized that it needs an ID column, unlike > > > HABTM tables). I am a big perfectionist and I frequently run "SHOW > > > COLUMNS IN table;", so I naturally expect to see the "id" column the > > > first one listed. However, if I just do a standard add_column() > > > migration, the column will appear on the bottom. Is there any way to > > > re-arrange this? The Rails docs says that there is an options > > > parameter for add_column, but I couldn''t really find much information > > > on it. I could, of course, manually write the SQL, but I want to see > > > if there is a way to do this built-in to add_column(). > > > > > [snip] > > I believe this is database dependent. Oracle, for instance, provides no > > way to do this other than to drop and recreate the table with the > > columns in the order you prefer. Ofcourse, when you need an ID column, > > you''re pretty much bound to be doing some SQL hackery anyway, as that > > will be a required (primary key) column by definition, and if your > > database table contains any data whatsoever.. You know :) > > > > Regards, > > Henning Kilset > > As Henning points out it''s totally dependant but you could always use > a specific adaptor within a migration and just manually run the query. > This would make the migration less useful if you decide to start using > another db type but in the short term it would work. > > If this is a concern about the future you could adjust your databases > manually for now and add the column in the original migration file (10 > migrations ago) and your future migrations will work nicely. > > The source is pretty specific about what options it takes and order > isn''t one of them. Actually, it''s the first time I''ve seen the docs > really, really groove with the source so perfectly. > > Cheers, > Chuck Vose > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Thanks for the responses. I just ended up manually creating it and removing the :id => false up in my 11th migration (heh, I completely forgot about auto_inc and primary key until Henning''s response; haven''t had to use it since I got on Rails).