如何删除主键?(How to drop a primary key?)

2019-10-18 11:41发布

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

并添加主键UID列,而不是让我omniauth工作。

schema.rb SNIPPIT

  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

我试着添加一个主键一起发表于:

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

并得到了以下错误(SNIPPIT):

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)

Answer 1:

你必须先删除现有主键,沿着线的东西:

ALTER TABLE "users" DROP CONSTRAINT "users_pkey"

另一个可以添加语句execute当前的一个在迁移之前。

编辑:您可以检查与下面的表格当前约束:

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

来源: PostgreSQL文档



文章来源: How to drop a primary key?