POSTGRES: измените тип первичного ключа с varchar на uuid и на него ссылаются другие tabless

у меня есть таблицы customer и user, я хочу изменить их идентификаторы с символа, изменяющегося на uuid.

=>d customers;
         Table "public.customers"
  Column  |       Type        | Modifiers
----------+-------------------+-----------
 id       | character varying | not null
 name     | character varying | not null
Indexes:
    "customers_pkey" PRIMARY KEY, btree (id)
    "customers_name_key" UNIQUE CONSTRAINT, btree (name)
Referenced by:
    TABLE "users" CONSTRAINT "users_customer_id_fkey" FOREIGN KEY (customer_id) REFERENCES customers(id) DEFERRABLE INITIALLY DEFERRED

=>d users;
             Table "public.users"
    Column     |       Type        | Modifiers
---------------+-------------------+-----------
 id            | character varying | not null
 name          | character varying | not null
 customer_id   | character varying | not null
 login         | character varying | not null
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)
    "users_login_key" UNIQUE CONSTRAINT, btree (login)
Foreign-key constraints:
    "users_customer_id_fkey" FOREIGN KEY (customer_id) REFERENCES customers(id) DEFERRABLE INITIALLY DEFERRED

Я пробовал:

=>begin;
set constraints all deferred;
ALTER TABLE users alter customer_id type uuid using customer_id::uuid;
ALTER TABLE customers alter id type uuid using id::uuid;
end;

но я получил несовместимую ошибку типа после этой строки

=> ALTER TABLE customers alter id type uuid using id::uuid;
ERROR:  foreign key constraint "users_customer_id_fkey" cannot be implemented
DETAIL:  Key columns "customer_id" and "id" are of incompatible types: character varying and uuid.

любая помощь будет оценили. Спасибо

1 ответов


вы определенно захотите удалить ограничение внешнего ключа. Я был удивлен, что вам не нужно было отбрасывать ограничение PK или индекс для PostgreSQL, чтобы изменить тип данных. Я установил тест, и он работал нормально, просто сначала нужно было сбросить FK, а затем снова добавить его позже.