'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.
Solution 1:[1]
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)
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | pbuck |