I am writing a script in which I fetch data from MongoDB and dump it into PostgreSQL. Everything works fine but when I try to insert the MongoDBs ObjectId _id, it gives the following error.
(psycopg2.errors.SyntaxError) trailing junk after numeric literal at or near "63711d"
I want to insert the _id in the PostgreSQL db as primary key, so that duplicate rows do not exist.
The query generated by psycopg2 is as follows
[SQL: INSERT INTO employees (_id, candidate_last_name, candidate_first_name, candidate_state, candidate_experience, candidate_relocation, candidate_skills, candidate_specialty)VALUES (6375364d1ad809ab6108a544, NULL, NULL, NULL, NULL, NULL, NULL, NULL), (6375364d1ad809ab6108a545, NULL, NULL, NULL, NULL, NULL, NULL, NULL)]
The _id field in PostgreSQL is VARCHAR.
The code that I am using is as follows:
def insert_psql(db, table_name: str, fields: dict, data: list):
new_fields = {}
for field in fields:
new_fields[field.replace('.', '_')] = fields[field]
insert_query = f'INSERT INTO {table_name} ('
insert_query += ', '.join(new_fields.keys()) + ')'
insert_query += 'VALUES '
for i, row in enumerate(data):
insert_query += '('
for j, field in enumerate(fields):
if not row.get(field):
insert_query += 'NULL'
else:
insert_query += f'{str(row.get(field))}'
if not j == len(fields) - 1:
insert_query += ', '
insert_query += ')'
if not i == len(data) - 1:
insert_query += ', '
# print(insert_query)
try:
db.execute(insert_query)
db.commit()
except Exception as e:
print(e)
The fields dict is a dictionary containing column names and their data_types as value. The data list a list of records to insert