I'm getting this error KeyError: 'country.id' when I use psycopg2 to insert a list with a nested dictionary into a table in postgres:
import psycopg2
import logging
from psycopg2.extras import LoggingConnection
def insert_fixture_data(match: dict):
conn = psycopg2.connect(
connection_factory=LoggingConnection, **db_settings)
home_team = match['home_team']
home_manager = home_team['managers']
sql_updates = (
"INSERT INTO manager (id,country_id,name,nickname,birth_date) VALUES (%(id)s,%(country.id)s,%(name)s,%(nickname)s,%(dob)s) ON CONFLICT DO NOTHING RETURNING id;"
)
try:
cursor = conn.cursor()
cursor.executemany(sql_updates,
home_manager)
except Exception as error:
print(error)
finally:
conn.close()
home_manager looks like this:
[{'id': 665, 'name': 'Ivan Juric', 'nickname': None, 'dob': '1975-08-25', 'country': {'id': 56, 'name': 'Croatia'}}]
The schema and column names were correct when I checked in postgresql.
country.idnotcountry_id. You are assumingcountry.idis going to select'country': {'id': ...and that is not the case.