2

I have the following Psql table structure.

Table "public.kart_user":

 Column   |          Type          | Modifiers | Storage  | Stats target | Description 
------------+------------------------+-----------+----------+--------------+-------------
 token      | character varying(35)  | not null  | extended |              | 
 cycle      | character varying(200) |           | extended |              | 
 userid     | character varying(200) |           | extended |              | 
 proxyid    | character varying(200) |           | extended |              | 
 salesrepid | character varying(200) |           | extended |              | 
 buyer      | character varying(200) |           | extended |              | 
 salesrep   | character varying(200) |           | extended |              | 
 discount   | integer[]              |           | extended |              | 
 custid     | character varying(200) |           | extended |              | 
 category   | character varying(200) |           | extended |              | 
 users      | character varying(500) |           | extended |              | 
 validto    | date                   |           | plain    |              | 
 ratioonly  | boolean                |           | plain    |              | 
 proxy      | character varying(500) |           | extended |              | 
 validfrom  | date                   |           | plain    |              | 
 notified   | boolean                |           | plain    |              | 
Indexes:
    "kartuser_pkey" PRIMARY KEY, btree (token)
Referenced by:
    TABLE "kart_space" CONSTRAINT "kart_space_token_fkey" FOREIGN KEY (token) REFERENCES kart_user(token)
    TABLE "kart_order" CONSTRAINT "kartorder_token_fkey" FOREIGN KEY (token) REFERENCES kart_user(token)

I'd like to change the column token from an varchar to a uuid without dropping the foreign key
I have tried the following, but failed.

ALTER TABLE kart_user alter token type uuid using token::uuid;

Note: I don't want to drop the relation ship in any case

How can i fix this issue? Thank you for your response

wildplasser
  • 43,142
  • 8
  • 66
  • 109
Abdul Razak
  • 2,654
  • 2
  • 18
  • 24
  • 1
    You can't, if you insist on not dropping the foreign keys. Why? Because the types of the *referencing* columns have to match the types of the *referenced* columns. You'd immediately have an invalid foreign key because the types don't match. – Damien_The_Unbeliever Dec 31 '15 at 07:24
  • without dropping a foreign key, I belive not – Vao Tsun Dec 31 '15 at 07:26
  • Possible duplicate of [Change primary key in PostgreSQL table](http://stackoverflow.com/questions/29075413/change-primary-key-in-postgresql-table) – Elad Dec 31 '15 at 08:02
  • BTW: you table looks very unnormalised to me. I suggest you squeeze out the {users,salesmen,customers, suppliers} into their own tables and refer to them by their ids. – wildplasser Dec 31 '15 at 11:31

0 Answers0