db:使用postgres迁移。如何修复用户已经存在

时间:2012-01-14 22:57:11

标签: ruby-on-rails-3 postgresql

在这里有另一个问题,我试图不向许多人询问,但要继续左右撞墙。使用postgres运行db:migrate并获得以下错误。如何修复用户已经存在?

$ rake db:migrate
==  CreateUsers: 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:

PGError: ERROR:  relation "users" already exists
: CREATE TABLE "users" ("id" serial primary key, "first_name" character varying(25),     "last_name" character varying(50), "email" character varying(255) DEFAULT '' NOT NULL,    "password" character varying(40), "created_at" timestamp, "updated_at" timestamp) 

Tasks: TOP => db:migrate

下面的rb文件

class CreateUsers < ActiveRecord::Migration
  def up
    create_table :users do |t|
      t.string "first_name", :limit => 25
      t.string "last_name", :limit => 50
      t.string "email", :default => "", :null => false
      t.string "password", :limit => 40
      t.timestamps
    end
  end

  def down
    drop_table :users
  end
end

1 个答案:

答案 0 :(得分:2)

您可以在create_table :users do |t|之前放置一个命令来删除表格drop_table :users。只有这样,如果你没有数据丢失!

如果您想为每行添加add_column列,例如。 add_column :users, :name, :string