2017-05-01 51 views
1

我填充像這樣的表:插入生成的UUID到表 - 無法適應型UUID

... 
time_id = uuid.uuid4() 
location_id = uuid.uuid4() 
id = row['id'] 

sql = ''' 
     INSERT INTO "my-schema"."example"(
      ... 
      time_id, 
      location_id 
      id 
     ) VALUES (..., %s, %s, %s) 
''' 
data = (..., time_id, location_id, id) 
try: 
    my_cursor.execute(sql, data) 
    my_conn.commit() 
except (psycopg2.Error) as e: 
    print(e) 

符合下表的定義:

CREATE TABLE "my-schema".example 
(
    ... 
    time_id character varying(50), 
    location_id character varying(50), 
    CONSTRAINT "PK_example" PRIMARY KEY (id) 
) 
WITH (
    OIDS=FALSE 
); 
ALTER TABLE "my-schema".example 
    OWNER TO postgres; 

但是這給了我以下錯誤:can't adapt type 'UUID'。這是什麼原因?我需要time_idlocation_id是唯一的,因爲它們充當維度和事實表之間的外鍵。

+1

是不是有沒有使用UUID數據類型存儲UUID的原因? https://www.postgresql.org/docs/9.1/static/datatype-uuid.html – Ivan

回答

2

uuid是一個特殊的類型,看起來像一個字符串,但不是。你試圖把它放到一個字符字段中。您需要先將其轉換爲字符串:str(time_id), str(location_id)

+0

完美,當時間限制到達時會接受回答。 – petermark