【问题标题】:Testing with rspec is causing an error: `Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development `使用 rspec 进行测试会导致错误:`迁移未决。要解决此问题,请运行: bin/rake db:migrate RAILS_ENV=development `
【发布时间】:2015-04-29 01:30:02
【问题描述】:

当我运行 rails 服务器时,localhost 显示此错误:
ActiveRecord::PendingMigrationError(迁移正在等待。 要解决此问题,请运行:bin/rake db:migrate RAILS_ENV=development):

我已经运行了 bin/rake... 并且下一个错误说:

 $ bundle exec bin/rake db:migrate RAILS_ENV=development  
  == 20150225172130 CreateVotes:
 migrating ======================================
-- create_table(:votes)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

  SQLite3::SQLException: table "votes" already exists: CREATE TABLE "votes" 
  ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "value" varchar(255), 
 "user_id" integer, "post_id" integer, "created_at" datetime, "updated_at"  
 datetime) 

_create_votes.rb

class CreateVotes < ActiveRecord::Migration
  def change
create_table :votes do |t|
  t.string :value
  t.references :user, index: true
  t.references :post, index: true

  t.timestamps
    end
  end
end

基本上,它告诉我我有待处理的迁移,但是当我尝试迁移时它说表已经存在。

20150225172130 CreateVotes: 
migrating ======================================
-- create_table(:votes)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "votes" already exists: CREATE TABLE "votes"...

运行 db:drop:all 之后 数据库:创建 数据库:迁移 数据库:测试:克隆

投票现在显示在 schema.rb 中:

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

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

  add_index "comments", ["post_id"], name: "index_comments_on_post_id"

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

  create_table "users", force: true do |t|
    t.string   "name"
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string   "unconfirmed_email"
    t.datetime "created_at"
    t.datetime "updated_at"
      end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name:"index_users_on_reset_password_token", unique: true

  create_table "votes", force: true do |t|
t.integer  "value"
t.integer  "user_id"
t.integer  "post_id"
t.datetime "created_at"
t.datetime "updated_at"
  end

  add_index "votes", ["post_id"], name: "index_votes_on_post_id"
  add_index "votes", ["user_id"], name: "index_votes_on_user_id"
        end

模型/投票.rb

class Vote < ActiveRecord::Base
  belongs_to :user
  belongs_to :post

end

【问题讨论】:

    标签: ruby-on-rails-3 activerecord sqlite database-migration


    【解决方案1】:

    你可以这样做:

    class CreateVotes < ActiveRecord::Migration
    
    
    def change
      unless table_exists? :votes
        create_table :votes do |t|
          t.string :value
          t.references :user, index: true
          t.references :post, index: true
    
          t.timestamps
        end
      end
    end
    

    我之前在迁移未按预期运行时遇到过这种情况。但是您还应该检查您的 schema.rb 文件以确保 votes 表具有迁移添加的列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 2015-04-01
      相关资源
      最近更新 更多