String values returned from Postgres in Python using psycopg2 have a value of "{ raw: true }" for some reason

Viewed 40

I've opened a cursor to make a query to my Postgres database:

    cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

I query a table:

    cur.execute("""select * from mcc_event_staging where state = 'NOT_LOADED'""")

I fetchAll() and iterate through the results as a dictionary. For each row, I grab these three values:

        clientId = row['detail.data.mcc_inbound_message.client_id']
        channelId = row['detail.data.mcc_inbound_message.channel_id']
        msgId = row['detail.data.mcc_inbound_message.msg_id']

Later in my code, after catching exceptions I have a debug statement that prints these values:

print("Load failed for clientId = %s, channelId = %s, msgId = %s" % (clientId, channelId, msgId))

I also add an update statement to change the status of the row in the staging table to "ERROR_ON_LOAD" for any errors with this:

cur.execute("""UPDATE mcc_event_staging SET state = 'ERROR_ON_LOAD' where client_id = %s and channel_id = %s and msg_id = %s;""" , (clientId, channelId, msgId,))

When the code executes, I can see my debug statement and it looks good:

Load failed for clientId = 12345, channelId = 95313e42-5285-43f0-8a68-9cbbae3d97bc, msgId = 4

Fantastic. But the SQL statement fails because of some kind of string parsing problem or something else that I'm not able to figure out:

Traceback (most recent call last):
  File "/var/task/app.py", line 134, in lambda_handler
    query()
  File "/var/task/app.py", line 96, in query
    cur.execute("""UPDATE mcc_event_staging SET state = 'ERROR_ON_LOAD' where client_id = { raw: true } and channel_id = { logID: 8424 } and msg_id = %s;""" , (clientId, channelId, msgId,))
  File "/var/task/psycopg2/extras.py", line 236, in execute
    return super().execute(query, vars)

Why are the values { raw: true } and { logID: 8424 } appearing for the values of variables that are appearing correctly in my debug statement?

Am I not parsing strings correctly for my execute() statement? What do these values even mean and how do I get the correct values for these strings to appear in the executed SQL?

Any help is greatly appreciated.

0 Answers
Related