所以,我想将当前的主键放在我的用户表上。我认为它在电子邮件列中。
然后在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)
答案 0 :(得分:1)
您必须首先删除现有的主键,类似于:
ALTER TABLE "users" DROP CONSTRAINT "users_pkey"
。
您可以在迁移前将该语句添加为当前语句之前的另一个execute
。
编辑:您可以使用以下内容检查表格上的当前约束:
select * from information_schema.table_constraints where
table_name='myTable';