Ruby on Rails:向现有数据库添加列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16259687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on Rails: adding columns to existing database
提问by hellomello
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
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?
知道为什么我会收到一个错误,说没有列吗?我还是 RoR 的新手。我是否必须以其他方式添加列?
Thanks
谢谢
回答by amb110395
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:
正如 Speransky 建议的那样,您永远不应该修改旧的迁移文件。相反,您应该创建一个添加所需列的新迁移。例如,在这种情况下,您将在您的应用程序中运行以下命令来创建新的迁移:
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.
Rails 会自动生成迁移文件,剩下要做的就是运行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).
这将破坏您当前的数据库,创建一个新数据库并运行所有迁移(其中将包括您的新列)。
回答by Edgar Wang
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:integerand then use rake db:migrateto commit this change.
如果要将新列添加到现有数据库,则应使用rails generate migration. 因此,您可以尝试rails generate migration add_list_id_to_ideas list_id:integer然后使用rake db:migrate来提交此更改。
回答by Danil Speransky
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 use rake db:migrate. If you run the migration with creating the table before, then you should create new migration where you may use add_columnmethod.
您不应向旧迁移添加新行。迁移是建立数据库的一个步骤。并且上次执行的迁移数存储在 中schema,如果您使用将使用rake db:migrate. 如果您之前通过创建表运行迁移,那么您应该在可以使用add_column方法的地方创建新的迁移。
回答by abo-elleef
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
迁移文件名在其名称中编码了日期时间,因此 rails 运行此迁移,除非您进行回滚,否则不要再次运行它
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
迁移的神奇之处就在这里,它可以通过小步骤构建您的数据库,因此在运行 rake db:migrate 后无需更新迁移,您应该进行新的迁移以对数据库架构进行所需的更改
and remember to
并记得
remove the added line form the old migration file as it might raise errors if you decided to rollback this migration
从旧迁移文件中删除添加的行,因为如果您决定回滚此迁移,它可能会引发错误
回答by Nikhil Thombare
Rails 4.0 easy way to add single or multiple columnhttps://gist.github.com/pyk/8569812
Rails 4.0添加单列或多列的简单方法https://gist.github.com/pyk/8569812
回答by aaquib
You can also do this ..
你也可以这样做..
rails g migration add_column_to_users list_id:string
rails g migration add_column_to_users list_id:string
then rake db:migrate
然后 rake db:migrate
also add :list_idattribute in your user controller ;
还add :list_id属性在您的用户控制器中;
for more detail check out http://guides.rubyonrails.org/active_record_migrations.html
有关更多详细信息,请查看http://guides.rubyonrails.org/active_record_migrations.html
回答by Marko
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
如果您的 migrate 文件夹中已经有文件,您可以在那里添加您想要的列(只需键入代码),删除 development.sqlite 或任何代表您的 db 文件的内容,然后运行 rake db:migrate。然后它将在表中创建一个带有新列的新 sqlite 文件,您可以在 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?
所以,基本上,你所做的一切看起来都很好,除了你没有删除你的数据库文件。这样做对我来说似乎是最简单的,尽管您会丢失数据库中的所有文件。如果您只是在测试和开发 Rails 应用程序,这很有效。除了我写的内容之外,任何人都可以评论这种方法是否有问题?
Edit: I actually found an answer about that here Editing Existing Rails Migrations is a good idea?
编辑:我实际上在这里找到了一个答案, 编辑现有的 Rails 迁移是个好主意?

