Ruby on Rails: adding columns to existing database

2019-03-08 05:58发布

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

7条回答
狗以群分
2楼-- · 2019-03-08 06:48

You can also do this ..

rails g migration add_column_to_users list_id:string

then rake db:migrate

also add :list_id attribute in your user controller ;

for more detail check out http://guides.rubyonrails.org/active_record_migrations.html

查看更多
登录 后发表回答