Ruby on rails 如何删除主键?

Ruby on rails 如何删除主键?,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 4,因此,我想将当前主键放在我的users表上。。我想它在电子邮件栏上 并将主键添加到uid列,以使omniauth正常工作 schema.rb snippit create_table "users", force: true do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: fa

因此,我想将当前主键放在我的users表上。。我想它在电子邮件栏上

并将主键添加到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
我已尝试使用以下命令向uid添加主键:

class ChangeUidToPrimaryKey < ActiveRecord::Migration
  def change
    execute 'ALTER TABLE users ADD PRIMARY KEY (uid)'
  end
end
那我怎么才能让它工作呢

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)

您必须先删除现有主键,大致如下:

更改表“用户”删除约束“用户”

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

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

select*from information\u schema.table\u约束,其中
表_name='myTable'


来源:

ah我明白了,但是我在哪里使用select*from information\u schema.table\u constraints where table\u name='myTable'?不在办公室工作commandline@TonyP你在什么环境下工作
rails dbconsole
从命令行获得PostgreSQL提示符。是的,但由于某种原因它不会返回任何结果:/you use teamviewer?@TonyP Nope no have teamviewer。您运行的命令末尾有一个分号,对吗?Psql在看到分号之前不会执行任何操作。@TonyP我猜您必须重新运行查询,因为Psql只是尝试运行您在分号之前键入的所有内容:)
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)