问题描述
我遇到错误:
SQLite3::SQLException: no such column: ideas.list_id:
SELECT "ideas".* FROM "ideas"
WHERE "ideas"."list_id" = 2
但是我添加了
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
这给了我:
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
然后我输入
rake db:migrate
有什么主意,为什么我会收到一个错误消息,说没有专栏?我还是RoR的新手.我是否必须以其他方式添加列?
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?
谢谢
推荐答案
如Speransky建议的那样,您永远不要修改旧的迁移文件.而是应创建一个新迁移,以添加所需的列.例如,在这种情况下,您将在应用程序中运行以下命令以创建新的迁移:
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
然后Rails会自动生成迁移文件,剩下要做的就是运行rake db:migrate
.
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).
这篇关于Ruby on Rails:向现有数据库添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!