続・migrationのadd_columnでafterオプションを付加する
http://d.hatena.ne.jp/foosin/20090428/1240914523
適当に試したらでけたのでメモ。
app/helpers/migration_helper.rb
module MigrationHelper def foreign_key(from_table, from_column, to_table) constraint_name = "fk_#{from_table}_#{to_table}" execute "alter table #{from_table} add constraint #{constraint_name} foreign key (#{from_column}) references #{to_table}(id)" end end module ActiveRecord module ConnectionAdapters # :nodoc: module SchemaStatements def add_column_options!(sql, options) #:nodoc: sql << " DEFAULT #{quote(options[:default], options[:column])}" if options_include_default?(options) # must explicitly check for :null to allow change_column to work on migrations if options[:null] == false sql << " NOT NULL" end if options[:after] sql << " AFTER #{options[:after]}" end end end end end
add_hoge_to_user.rb
class AddHogeToUser < ActiveRecord::Migration extend MigrationHelper def self.up add_column :users, :hoge, :string, :after => 'name' end def self.down remove_column :users, :hoge end end
外部キーにインデックス張るのも、カラム追加時にafter付けるのも、絶対使うと思うんだけど標準で用意されていないってことは皆は使わないって事なのか。