Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting generated UUID into table - can't adapt type UUID

I'm populating a table like so:

...
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)

With the following table definition:

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;

However this gives me the following error: can't adapt type 'UUID'. What would the reason for this be? I need the time_id and location_id to be unique as they act as a foreign key between a dimension and fact table.

like image 753
petermark Avatar asked May 01 '17 17:05

petermark


1 Answers

uuid is a particular type which looks like a string, but isn't. You're trying to put it into a character field. You need to convert it to a string first: str(time_id), str(location_id).

like image 57
pbuck Avatar answered Oct 14 '22 04:10

pbuck