Ruby on Rails: adding columns to existing database

早过忘川 提交于 2019-12-02 14:56:38
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:

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).

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.

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_column method.

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

Rails 4.0 easy way to add single or multiple column https://gist.github.com/pyk/8569812

aaquib

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

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

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!