8

It seems strange to me, that creation of model, running migration, destroying it, and creating again same model reports SQL exception:

project|master ⇒ rails g model name name
      invoke  active_record
      create    db/migrate/20130417185814_create_names.rb
      create    app/models/name.rb
project|master⚡ ⇒ rake db:migrate
==  CreateNames: migrating ====================================================
-- create_table(:names)
   -> 0.0020s
==  CreateNames: migrated (0.0021s) ===========================================
project|master⚡ ⇒ rails d model name
      invoke  active_record
      remove    db/migrate/20130417185814_create_names.rb
      remove    app/models/name.rb
project|master⚡ ⇒ rake db:migrate
project|master⚡ ⇒ rails g model name test
      invoke  active_record
      create    db/migrate/20130417185845_create_names.rb
      create    app/models/name.rb
project|master⚡ ⇒ rake db:migrate
==  CreateNames: migrating ====================================================
-- create_table(:names)
rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "names" already exists: CREATE TABLE "names" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "test" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /path/project/db/migrate/20130417185845_create_names.rb:3:in `change'
-- create_table("names", {:force=>true})
   -> 0.0100s
-- initialize_schema_migrations_table()
   -> 0.0025s
-- assume_migrated_upto_version(20130417185814, ["/path/project/db/migrate"])
   -> 0.0010s
You have 1 pending migrations:
  20130417185845 CreateNames
Run `rake db:migrate` to update your database then try again.

Maybe, I doing something wrong? Migration has code for deleting table - does it may be used only for rollback?

Solution

Delete model and database table and generate a new one is pretty easy:

  1. Create model: rails g model user name
  2. Do migrations: rake db:migrate
  3. Implement something, suddenly remember that you need to delete model
  4. Revert specific migration: rake db:migrate:down VERSION=20130417185814, where 20130417185814 is migration id (can be seen in rake db:migrate:status)
  5. Remove model: rails d model user
  6. Suddenly remember that you need this model, but with other fields
  7. Create model: rails g model user email group:references
  8. Successfully migrate database: rake db:migrate
Roman C
  • 49,761
  • 33
  • 66
  • 176
Drakmail
  • 682
  • 2
  • 7
  • 22

3 Answers3

9
rails d model name 

This just deletes the model and not the migration you have run (which created the table in the database).

If you want to delete both the model and the tables, you will have to do the following

rake db:rollback 
rails d model name
RodgerB
  • 8,608
  • 9
  • 36
  • 47
Nishant
  • 2,975
  • 23
  • 38
4

You deleted the model–that's a different operation than rolling back a migration.

Destroying a model does precisely, and only, that; it has nothing to do with migrations.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Does I need to create separate migration for deleting table? – Drakmail Apr 17 '13 at 20:22
  • @DRakmail why wasn't it in your original one? – Dave Newton Apr 17 '13 at 20:51
  • Because original one was deleted by rails d model ModelName :) But you gave me an idea. Now i see this workflow: 1. Create model 2. Migrate 3. `rake db:migrate:down VERSION=20130417185845`, where 20130417185845 is migration version 4. Delete model (`rails d model ModelName`) 5. Now I can again create model with the same name Does this way is right? – Drakmail Apr 17 '13 at 21:26
0

according to your migration error there must be something wrong with the migration files, moreover the one which referring to create names table.

please look at this file, at your change method.

the change method in a migration file is supposed to execute a DB code, that DB code can do some operations on the DB, and that same code on the change supposed to be right to do the rolling back.

if you want to separate between the two you should put the code on the up method which will perform operations on the DB, and on the down method the opposite rolling operations.

i would suggest that you delete all the files on the migration including the one that cause the problem, and write them correct.

if you need help please post your migration file.

please also take a look at the guides: http://guides.rubyonrails.org/migrations.html

  • I don't edit any migrations - you can try to execute this commands: `rails g model name name` `rake db:migrate` `rails d model name` `rake db:migrate` `rails g model name test` `rake db:migrate` And see my result. – Drakmail Apr 17 '13 at 20:18