迁移显示为已关闭但已迁移?

时间:2013-09-18 17:20:37

标签: ruby-on-rails ruby-on-rails-3 rails-migrations

我的rails应用程序有一些迁移。当我点击rake db:migrate:status查看已设置的状态时,********** NO FILE **********以外的所有状态均已关闭。但是迁移已经发生了,因此模型方面似乎没有问题。这些文件可能有助于解释:

rake db:migrate:status的输出:

 up     20130727003912  ********** NO FILE **********
down    20130728000151  Devise create users
down    20130728000335  Create friends
down    20130728000346  Create addresses
down    20130728000356  Create authies
down    20130728000413  Add indexes

此时,rake db:migrate输出:

==  DeviseCreateUsers: migrating ==============================================
-- create_table(:users)
NOTICE:  CREATE TABLE will create implicit sequence "users_id_seq1" for serial column "users.id"
rake aborted!
An error has occurred, this and all later migrations canceled:

PG::DuplicateTable: ERROR:  relation "users" already exists
: CREATE TABLE "users" ("id" serial primary key, "username" character varying(255), "email" character varying(255) DEFAULT '' NOT NULL, "encrypted_password" character varying(255) DEFAULT '' NOT NULL, "reset_password_token" character varying(255), "reset_password_sent_at" timestamp, "remember_created_at" timestamp, "sign_in_count" integer DEFAULT 0, "current_sign_in_at" timestamp, "last_sign_in_at" timestamp, "current_sign_in_ip" character varying(255), "last_sign_in_ip" character varying(255), "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL) /home/ekrem/workspace/contactman/db/migrate/20130728000151_devise_create_users.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

db/schema.rb

ActiveRecord::Schema.define(:version => 20130727003912) do

  create_table "addresses", :force => true do |t|
    t.string   "title"
    t.text     "address"
    t.string   "phone"
    t.string   "city"
    t.integer  "friend_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.string   "slug"
    t.string   "country"
  end

  add_index "addresses", ["friend_id"], :name => "index_addresses_on_friend_id"
  add_index "addresses", ["slug"], :name => "index_addresses_on_slug", :unique => true

  create_table "authies", :force => true do |t|
    t.string   "provider"
    t.string   "uid"
    t.integer  "user_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  add_index "authies", ["user_id"], :name => "index_authies_on_user_id"

  create_table "friends", :force => true do |t|
    t.string   "name"
    t.string   "surname"
    t.integer  "user_id"
    t.datetime "created_at",            :null => false
    t.datetime "updated_at",            :null => false
    t.string   "slug"
    t.string   "imported_file_name"
    t.string   "imported_content_type"
    t.integer  "imported_file_size"
    t.datetime "imported_updated_at"
  end

  add_index "friends", ["slug"], :name => "index_friends_on_slug", :unique => true
  add_index "friends", ["user_id"], :name => "index_friends_on_user_id"

  create_table "users", :force => true do |t|
    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
    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.datetime "created_at",                             :null => false
    t.datetime "updated_at",                             :null => false
    t.string   "username"
  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

end

如何修复此问题以使所有迁移都显示出来?

3 个答案:

答案 0 :(得分:6)

您的用户表已在数据库中创建,但您的架构迁移未使用最新迁移进行更新。我不知道为什么它没有更新。您需要修复它,然后您可以通过两种方式执行此操作1.删除数据库并再创建一个或2.运行向下迁移然后向上迁移

rake db:drop
rake db:create
rake db:migrate

答案 1 :(得分:0)

您可以将force设置为true:

create_table :users, force: true do
   ...
end
在迁移中

并手动删除“schema_migrations”表中的迁移密钥,然后重新运行迁移。

答案 2 :(得分:0)

这对我来说非常有效:

rails db:drop
rails db:create
rails db:migrate

您会注意到文件 developmen.sqlite3 已被删除并重新创建,现在一切都将启动并运行 rails db:migrate:status