2

所以,我想在我的用户表上删除当前的主键。我认为它在电子邮件列上。

并将主键添加到 uid 列以使我的omniauth 正常工作。

schema.rb 片段

  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"
    t.datetime "updated_at"
    t.boolean  "admin"
    t.string   "provider"
    t.string   "uid"
    t.string   "username"
  end

我尝试使用以下方法向 uid 添加主键:

class ChangeUidToPrimaryKey < ActiveRecord::Migration
  def change
    execute 'ALTER TABLE users ADD PRIMARY KEY (uid)'
  end
end

并得到以下错误(片段):

PG::Error: ERROR:  multiple primary keys for table "users" are not allowed
: ALTER TABLE users ADD PRIMARY KEY (uid)/usr/local/rvm/gems/ruby-2.0.0-p195/gems/act

那么我怎样才能得到这个工作呢?

leap2_stage_development=# select * from information_schema.table_constraints where table_name='users';
   constraint_catalog    | constraint_schema |    constraint_name    |      table_catalog      | table_schema | table_name | constraint_type | is_deferrable | initially_deferred 
-------------------------+-------------------+-----------------------+-------------------------+--------------+------------+-----------------+---------------+--------------------
 leap2_stage_development | public            | users_pkey            | leap2_stage_development | public       | users      | PRIMARY KEY     | NO            | NO
 leap2_stage_development | public            | 2200_33435_1_not_null | leap2_stage_development | public       | users      | CHECK           | NO            | NO
 leap2_stage_development | public            | 2200_33435_2_not_null | leap2_stage_development | public       | users      | CHECK           | NO            | NO
 leap2_stage_development | public            | 2200_33435_3_not_null | leap2_stage_development | public       | users      | CHECK           | NO            | NO
(4 rows)
4

1 回答 1

2

您必须首先删除现有的主键,类似于:

ALTER TABLE "users" DROP CONSTRAINT "users_pkey".

execute您可以在迁移中的当前语句之前将该语句添加为另一个语句。

编辑:您可以使用以下内容检查表上的当前约束:

select * from information_schema.table_constraints where table_name='myTable';

资料来源:PostgreSQL 文档

于 2013-08-10T03:17:46.373 回答