I have a postgresql table:
create table if not exists public.test
(
id integer,
name varchar,
months jsonb
);
I have a json:
[
{
"id": 1,
"name": "name_tset",
"months": [
{
"month": 7,
"year": 2024,
"days_in_month": 29,
"fact": "0",
}
]
},
...
]
When I want to insert data, the error "can't adapt type 'dict'" appears. I know it's because of the "month" dictionary list, but how do I add my code to make it work? Without "month", the insertion into the table is successful.
My code:
for d in data:
columns_list = list(d.keys())
insert_query = sql.SQL("INSERT INTO {schema}.{table_name} ({columns}) VALUES ({values})").format(
schema=sql.Identifier(f'{schema}'),
table_name=sql.Identifier(f'{table_name}'),
columns=sql.SQL(",").join(map(sql.Identifier, columns_list)),
values=sql.SQL(",").join(map(sql.Placeholder, columns_list))
)
cursor.execute(insert_query, d)