Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: adding columns to existing database

Tags:

I'm getting an error:

SQLite3::SQLException: no such column: ideas.list_id: 
SELECT "ideas".* FROM "ideas"  
WHERE "ideas"."list_id" = 2

But I added

t.integer :list_id

to my db migration file:

class CreateIdeas < ActiveRecord::Migration
  def change
    create_table :ideas do |t|
      t.string :name
      t.text :description
      t.string :picture

      t.timestamps
    end
    add_foreign_key :ideas, :lists
  end
end

which gave me this:

class CreateIdeas < ActiveRecord::Migration
  def change
    create_table :ideas do |t|
      t.string :name
      t.text :description
      t.string :picture
      t.integer :list_id
      t.timestamps
    end
    add_foreign_key :ideas, :lists
  end
end

and then I typed

rake db:migrate

Any idea why I would be getting an error saying there's no column? I'm still new to RoRs. Do I have to add a column some other way?

Thanks

like image 532
hellomello Avatar asked Apr 28 '13 04:04

hellomello


People also ask

How do I add a column to a database in Ruby on Rails?

If you want to add a new column to an exist database, you should use rails generate migration . So you can try rails generate migration add_list_id_to_ideas list_id:integer and then use rake db:migrate to commit this change.

How do you add multiple columns in rails?

command to create new model and table with columns : rails g model ModelName col_name1:string col_name2:integer col_name3:text ... command to add more columns under existing table: rails g migration AddColumnToModelName col_name4:string col_name5:integer ...

How do I add a reference column in rails?

When you already have users and uploads tables and wish to add a new relationship between them. Then, run the migration using rake db:migrate . This migration will take care of adding a new column named user_id to uploads table (referencing id column in users table), PLUS it will also add an index on the new column.


2 Answers

As Speransky suggested, you should never modify old migration files. Rather you should create a new migration that adds the desired column. For instance, in this case you would run the following command in your app to create the new migration:

rails generate migration AddListIdColumnToIdeas list_id:integer

And Rails would generate the migration file automatically and the only thing left to do is run rake db:migrate.

If you insist on modifying the old migration file, you can add the column as you did and run the following:

rake db:drop
rake db:create
rake db:migrate

Which will destroy your current database, create a new one and run all the migrations (which will include your new column).

like image 83
amb110395 Avatar answered Oct 24 '22 03:10

amb110395


If you want to add a new column to an exist database, you should use rails generate migration. So you can try rails generate migration add_list_id_to_ideas list_id:integer and then use rake db:migrate to commit this change.

like image 30
Edgar Wang Avatar answered Oct 24 '22 03:10

Edgar Wang