2015-05-10 231 views
0

我下面从jumpstartlab.com,我有问题的博客教程..迁移错误

这是在控制台的错误信息,我得到的,当我尝试运行rake db:migrate

SQLite3::SQLException: table tags already exists: CREATE TABLE "tags" 
("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), 
"created_at" datetime, "updated_at" datetime) /home/aki/.rvm/gems/ruby- 
2.1.3/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `initialize 

我怎样才能删除表tags?我应该删除迁移文件吗?

这是schema.rb:

ActiveRecord::Schema.define(version: 20150506111021) do 

    create_table "articles", force: true do |t| 
    t.string "title" 
    t.text  "body" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "comments", force: true do |t| 
    t.string "author_name" 
    t.text  "body" 
    t.integer "article_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    add_index "comments", ["article_id"], name: "index_comments_on_article_id" 

    create_table "taggings", force: true do |t| 
    t.integer "tag_id" 
    t.integer "article_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    add_index "taggings", ["article_id"], name: "index_taggings_on_article_id" 
    add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id" 

    create_table "tags", force: true do |t| 
    t.string "name" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

end 

这是...create_tags.rb迁移文件:

class CreateTags < ActiveRecord::Migration 
    def change 
    create_table :tags do |t| 
     t.string :name 

     t.timestamps 
    end 
    end 
end 

这是...create_taggings.rb迁移文件:

class CreateTaggings < ActiveRecord::Migration 
    def change 
    create_table :taggings do |t| 
     t.references :tag, index: true 
     t.references :article, index: true 

     t.timestamps 
    end 
    end 
end 

如果您需要任何其他文件,我会更新我的问题。谢谢!!

+0

尝试'耙分贝:降:all',然后尝试'耙分贝:migrate' – Sontya

回答

0

rake db:migrate命令试图创建一个已经存在于您的数据库中的表。 将会有一个create_tags迁移文件已经被执行。 这就是为什么,因为tags表已经创建,所以它给出了错误。

您可以通过

rake db:drop 

删除整个数据库,然后创建一个新的DATABSE

rake db:create 

,然后运行迁移

rake db:migrate 

产生迁移文件通过此命令

rails g migration DropTags 

然后编辑代码,因为这

class DropTags < ActiveRecord::Migration 
    def up 
     drop_table :tags  
    end 
end 
+0

谢谢,它的工作。但你也许知道什么是问题?为什么会发生? – 11223342124

+0

查看我更新的答案 – Sontya