ruby-on-rails – 迁移后删除rails模型

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 迁移后删除rails模型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对我来说,创建模型,运行迁移,销毁它,再次创建相同的模型报告sql异常似乎很奇怪:
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.

也许,我做错了什么?迁移具有删除表的代码 – 它是否仅用于回滚?

删除模型和数据库表并生成一个新表非常简单:

>创建模型:rails g model用户名
>迁移:rake db:migrate
>实现一些东西,突然想起你需要删除模型
>恢复特定迁移:rake db:migrate:down VERSION = 20130417185814,其中20130417185814是迁移ID(可以在rake db:migrate:status中看到)
>删除模型:rails d model user
>突然想起你需要这个模型,但是需要其他领域
>创建模型:rails g model用户电子邮件组:引用
>成功迁移数据库:rake db:migrate

解决方法

rails d model name

这只是删除模型而不是您运行的迁移(在数据库中创建了表).

如果要删除模型和表,则必须执行以下操作

rake db:rollback 
rails d model name
原文链接:https://www.f2er.com/ruby/270476.html

猜你在找的Ruby相关文章