views:

43

answers:

1

I want to add STI to an existing table using a custom type column. Let's call this taste_type whose corresponding model is Fruit.

In the Fruit model I have:

set_inheritance_column :taste_type

In my migration to add STI I have:

class AddSTI < ActiveRecord::Migration
  def self.up
    add_column :fruits, :taste_type, :string, :limit => 100, :null => false
    Fruit.reset_column_information
    Fruit.find_by_id(1).update_attributes({:taste_type => 'Sour'})
  end

  def self.down
    remove_column :fruits, :taste_type
  end

end

When I run the migration, I get the following error:

Mysql::Error: Column 'taste_type' cannot be null: ...

Any idea what's going? I can get the migration to run if I comment the set_inheritance_column in the Fruit model, then uncomment it after I run the migration. Obviously, I don't want to do this, however.

A: 

The taste_type column can't be null. The DB throws an error because you are adding a new column(that can't be null) to a table with existing rows.

One way to work around this problem is to add a default value to the column and subsequently reset the default value.

add_column :fruits, :taste_type, :string, :limit => 100, :null => false, 
      :default => "Sour"
change_column :fruits, :taste_type, :string, :limit => 100, :null => false

Fruit.reset_column_information
Fruit.find_by_id(1).update_attributes({:taste_type => 'Sour'})

Other way is to run the migration after truncating the fruits table.

KandadaBoggu
Or if possible, re-seed your data with non-null fields.
Ryan Bigg
User is adding a new non null column to a table with data. He wont be able to get past the `add_column` if the new column is not nullable.
KandadaBoggu