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
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:
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:
Which will destroy your current database, create a new one and run all the migrations (which will include your new column).
You should not add new rows to old migrations. Migration is a step of building database. And number of last executed migration is stored in
schema
, and it will not be run or redone if you use will userake db:migrate
. If you run the migration with creating the table before, then you should create new migration where you may useadd_column
method.Rails 4.0 easy way to add single or multiple column https://gist.github.com/pyk/8569812
migration file name has the datetime encoded in its name so rails run this migration one and do not run it again unless you do a rollback
and here come the magic of migration to build you db with small steps so no need to update a migration after run rake db:migrate , you should make a new migration to do the change you want to your db schema
and remember to
remove the added line form the old migration file as it might raise errors if you decided to rollback this migration
If you want to add a new column to an exist database, you should use
rails generate migration
. So you can tryrails generate migration add_list_id_to_ideas list_id:integer
and then userake db:migrate
to commit this change.If you already have files in your migrate folder, you could just add column you want there(just type the code), delete development.sqlite or whatever represents your db file, and run rake db:migrate. It will then create a new sqlite file with new column in table, and you can check it in schema.rb
So, basically, everything you did seems good, except you didn't delete your database file. Doing this seems the easiest for me, all though you will lose all the files in your database. If you're just testing and developing Rails app, this works. Can anyone comment if there is something wrong with this approach, besides what i wrote?
Edit: I actually found an answer about that here Editing Existing Rails Migrations is a good idea?