website design and development

Weblog - 1 items for migrations

View all
litespeed hosting Ruby on Rails cpanel smtp plesk port 26 email DNS spam rss IE6 float CSS gmail ActiveRecord has many through plugins news subversion migrations shell webmin mail IE7 ssl

Ruby on Rails Making string primary key work for tests

10 Apr 2008 Ruby on Rails  migrations

If you want to have primary keys with a type of :string in Ruby on Rails you can do so by using the change_column method after creating a table in the migration.

eg.

create_table "g_languages", :force => true do |t|
t.column "name", :string, :limit => 50, :default => "", :null => false
end
change_column "g_languages", :id, :string, {:limit => 5, :default => "", :null => false}



Problem is if you want to use this Model in your tests the id is autmatically set to :integer and youre string change does not come accross. (The schema.rb is created without any id/primary key info so :integer is assumed)

The fix
Alter the table using ActiveRecord::Migration in test_helper.rb before the fixtures are loaded.

eg.

migrater = ActiveRecord::Migration
migrater.change_column :g_languages, :id, :string, {:limit => 5, :default => "", :null => false}


fixtures :all

© Level Systems 2007 Email: info@levelsystems.net   Port: +33 (0) 6 09 81 06 00   Tel: +33 (0) 9 70 46 63 37