views:

1141

answers:

7

If I'm adding a column via MySQL, I can specify where in the table that column will be using the AFTER modifier. But if I do the add_column via a Rails migration, the column will be created at the end of the table.

Is there any functionality for rails migrations to specify the position of an added column?

+2  A: 

I do not think there is now but more to the point, why do you need to specify the exact place?

Keltia
I thought the same thing but assumed the OP wants to iterate over columns in the result set, and have them come out in a predictable order.
Bill Karwin
You can specify the order of columns in a SQL query (not when using SELECT * of course) by giving the columns explicitely.
Keltia
Yes but when using an ORM layer, we seldom have the opportunity to specify columns. Rails' ActiveRecord is well-known for fetching all columns. This is a performance hit when it fetches large BLOBs that are not needed, for example.
Bill Karwin
+2  A: 

There does not seem to be a position option for the add_column method in migrations. But migrations support executing literal SQL. I'm no Rails developer, but something like the following:

class AddColumnAfterOtherColumn < ActiveRecord::Migration
  def self.up
    execute "ALTER TABLE table_name ADD COLUMN column_name INTEGER 
      AFTER other_column"
  end

  def self.down
    remove_column :table_name, :column_name
  end
end
Bill Karwin
+2  A: 

There is no way within Rails to specify the position of a column. In fact, I think it's only coincidental (and therefore not to be relied on) that columns are created in the order they are named in a migration.

The order of columns within a table is almost relevant and should be so: the common "reason" given is to be able to see a particular subset when executing a "SELECT *", but that's really not a good reason.

Any other reason is probably a design smell, but I'd love to know a valid reason why I'm wrong!

On some platforms, there is a (miniscule) space and performance saving to be obtained by putting the columns with the highest probability of being NULL to the end (because the DMBS will not use any disk space for "trailing" NULL values, but I think you'd have to be running on 1980's hardware to notice.

Mike Woodhouse
It's not coincidental, it's standard SQL behavior that ALTER TABLE ADD COLUMN adds the column as the last ordinal position in the table. MySQL's "AFTER" syntax is an extension to standard SQL.
Bill Karwin
ANSI standard? Or de facto? Not quibbling, just curious.
Mike Woodhouse
I'm going by the book "SQL-99 Complete, Really" which describes it as ANSI standard behavior that additional columns are added as the rightmost column in a table.
Bill Karwin
+2  A: 

I created a patch that adds this additional functionality to the ActiveRecord Mysql adapter. It works for master and 2-3-stable.

https://rails.lighthouseapp.com/projects/8994/tickets/3286-patch-add-support-for-mysql-column-positioning-to-migrations

It might be mysql specific, but it doesn't make your migrations any less portable (other adapters would just ignore the extra positioning options).

Ben Marini
A: 

A well-defined column order is also useful when you have database dumps (e.g. backups) and create a new database using your migrations and then want to fill the new database with data from the dumps.

Chris
A: 

as well for merged tables in mysql

table merger
+2  A: 

This is now possible in Rails 2.3.6+ by passing the :after parameter

https://rails.lighthouseapp.com/projects/8994/tickets/3286-patch-add-support-for-mysql-column-positioning-to-migrations

To everyone that doesn't see the advantage in having this feature: do you never look at your database outside of the ORM? If I'm viewing in any sort of UI, I like having things like foreign keys, status columns, flags, etc, all grouped together. This doesn't impact the application, but definitely speeds up my ability to review data.

Gabe Martin-Dempesy
Very cool new feature.
yalestar