# Add Column After Column in Migrations

**URL:** <https://www.ruby-forum.com/t/add-column-after-column-in-migrations/57419>\
**Category:** Rails\
**Created:** [April 10, 2006, 8:06pm UTC](https://www.ruby-forum.com/t/add-column-after-column-in-migrations/57419 "2006-04-10T20:06:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stimpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@stimpy](https://www.ruby-forum.com/u/stimpy)\
**Post date:** [April 10, 2006, 8:06pm UTC](https://www.ruby-forum.com/t/add-column-after-column-in-migrations/57419/1 "2006-04-10T20:06:13Z")

</div>

Adding a column to a table just places the column at the end, but I  
like to keep related columns together. Is there a way to specify  
where the column is added in a table with migrations? For example, in  
MySQL you can do:

ALTER TABLE my\_table ADD COLUMN new\_column INT AFTER other\_column

I was hoping migrations would support something like this:

add\_column :my\_table, :new\_column, :integer, :after =\> :other\_column

I couldn’t find anything mentioned in the documentation or in a quick  
search of this mailing list.

Thanks,

Ryan

---

<div class="post-metadata">

**Author:** ![stimpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@stimpy](https://www.ruby-forum.com/u/stimpy)\
**Post date:** [April 11, 2006, 8:00am UTC](https://www.ruby-forum.com/t/add-column-after-column-in-migrations/57419/2 "2006-04-11T08:00:25Z")

</div>

Probably not, because the MySQL syntax is probably not standard SQL.

It’s not directly possible in Oracle, PostGres, SQL Server, DB2, etc.,  
but if you need to, it is possible with a couple of different  
functions that select the fields you want into a new table with some  
default datatype for the field you’re adding (something like this in  
SQL Server):

select field\_a, field\_b, null as new\_field, field\_c, field\_d  
into #mytable\_temp  
from mytable;

truncate table mytable;

drop table mytable;

select \*  
into mytable  
from #mytable\_temp;

drop table #mytable\_temp;

alter table mytable alter new\_field varchar(200);

Or, use a view on the table instead, with the fields ordered how you  
like them ordered in the view. With most of the RDBMSs, this should  
work just fine.
