user4362206
user4362206

Reputation:

What to do when I remake model

When I want to remake a model, I run rails d model X and run rails g model X .... However, when I run rake db:migrate, it fails and console says 'table X already exists'.

I found that it works if I manually rewrite schema.rb.

Is this a right way? Are there other things to do(ex. to rewrite) when I destroy and create same model?

Upvotes: 0

Views: 108

Answers (2)

Tom Prats
Tom Prats

Reputation: 7921

You're looking for a migration. You don't need to delete your model to add additional data to it. http://guides.rubyonrails.org/active_record_migrations.html#creating-a-migration

You want to do something like this:

rails generate migration AddEmailToUsers email:string # In the command line

This generates a file in db/migrations/

class AddEmailToUsers < ActiveRecord::Migration
  def change
    add_column :users, :email, :string
  end
end

Then you run rake db:migrate and it will update your table

Upvotes: 1

Owlet
Owlet

Reputation: 49

The problem here is that the db already exists, even though you delete and create the Model. To delete the existing db you could just kill it, by deleting the development.sqlite file in your db folder.

Upvotes: 0

Related Questions