Rails: I update migration file then run db:migrate, but my schema isn't updating

前端 未结 7 1241
生来不讨喜
生来不讨喜 2020-12-24 13:27

I\'m trying to add an extra field to one of my tables.

I\'ve added the field in the migration file (under db\\migrate), then ran \'rake db:migrate\' which ran withou

相关标签:
7条回答
  • 2020-12-24 13:54

    http://guides.rubyonrails.org/migrations.html#changing-existing-migrations

    Occasionally you will make a mistake when writing a migration. If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run rake db:migrate. You must rollback the migration (for example with rake db:rollback), edit your migration and then run rake db:migrate to run the corrected version.

    0 讨论(0)
  • 2020-12-24 13:58

    You should always create a new migration file when adding/changing something in the database. This is the purpose of migrations. A migration file should have the ability to make the new change and undo the change. This way if something goes wrong or you changed your mind you can easily roll back to a previous migration.

    The following link's sections labeled 'Anatomy of a Migration' and 'Writing a Migration' might be of help to you.

    http://guides.rubyonrails.org/migrations.html

    0 讨论(0)
  • 2020-12-24 14:00

    I was able to regenerate my schema with latter migrations by running rake db:schema:dump

    0 讨论(0)
  • 2020-12-24 14:00

    Once u do rake db:migrate then again u cannot add column to that file.You have to generate a new migration file by using rails g migration add_columnname_to_tablename for adding that particular column and do rake db:migrate.That's it !!!

    0 讨论(0)
  • 2020-12-24 14:01

    Solved my own Question..

    Basically rather than edit the original migration files generated when you run scaffolding you create a new migration file just for what you want to acheive:

    http://guides.rubyonrails.org/migrations.html#creating-a-standalone-migration

    0 讨论(0)
  • 2020-12-24 14:10

    I did the same thing, I wanted to change a field name and instead of this:

    class CreateComments < ActiveRecord::Migration
      def change
        create_table :comments do |t|
          t.string :commenter
          t.text :body
    
          # this line adds an integer column called `article_id`.
          t.references :article, index: true
    
          t.timestamps
        end
      end
    end
    

    I changed

      t.text :body
    

    to

    t.text :comment_body
    

    I tried doing rake

    db:migrate
    

    nothing happened as in it went to the command prompt again without any output..., i looked at stack overflow and this guided me to do rake

    db:migrate:redo
    

    with out put

    == 20141129044056 CreateComments: reverting ===================================
    -- drop_table(:comments)
       -> 0.0000s
    == 20141129044056 CreateComments: reverted (0.0886s) ==========================
    
    == 20141129044056 CreateComments: migrating ===================================
    -- create_table(:comments)
       -> 0.0040s
    == 20141129044056 CreateComments: migrated (0.0040s) ==========================
    

    and then I loaded my page/controller with commenter_body instead of body and it loaded perfectly.

    I think this is also a solution to the same. I dont know if there is any issue in the underneath workings in the model/DB( i am still very new to RoR,my third day actually...)

    0 讨论(0)
提交回复
热议问题