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?
Delete model and database table and generate a new one is pretty easy:
rails g model user name
rake db:migrate
rake db:migrate:down VERSION=20130417185814
, where 20130417185814
is migration id (can be seen in rake db:migrate:status)rails d model user
rails g model user email group:references
rake db:migrate
【Ruby on Rails】 When you want to delete a migration file you made, you can choose version and delete it. You don't need to execute rails db:rollback many times!
To undo a rails generate command, run a rails destroy command. You can then edit the file and run rake db:migrate again. (See how to roll back a Migration file to rollback a specific migration or multiple migrations.)
1.3 Changing MigrationsIf 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With