tlocke / pg8000

A Pure-Python PostgreSQL Driver
BSD 3-Clause "New" or "Revised" License
515 stars 46 forks source link

[1.22.0] Unable to use prepared statement #89

Closed asvinours closed 2 years ago

asvinours commented 3 years ago

Hello,

Since the release of version 1.22.0, we're facing issues with prepared statements. The same code works fine on 1.21.3 but fails on 1.22.0 with the error below.

I did not look into it at all yet, but I figured I'd report it anyway.

 Traceback (most recent call last):
   File "/home/user/.local/lib/python3.9/site-packages/catalog_updater/update_catalog.py", line 526, in update_product_attributes
     text_id = attr_mgmt.get_text_attribute_by_id(attr.database_id, product_id)
   File "/home/user/.local/lib/python3.9/site-packages/catalog_updater/models/attribute_manager.py", line 109, in get_text_attribute_by_id
     res = self.prepared_statements['text_attribute_by_id'].run(
   File "/home/user/.local/lib/python3.9/site-packages/pg8000/native.py", line 223, in run
     self._context = self.con.execute_named(
   File "/home/user/.local/lib/python3.9/site-packages/pg8000/core.py", line 683, in execute_named
     self.handle_messages(context)
   File "/home/user/.local/lib/python3.9/site-packages/pg8000/core.py", line 764, in handle_messages
     raise self.error
 pg8000.exceptions.DatabaseError: {'S': 'ERROR', 'V': 'ERROR', 'C': '26000', 'M': 'prepared statement "pg8000_statement_1" does not exist', 'F': 'prepare.c', 'L': '515', 'R': 'FetchPreparedStatement'}
def prepare_text_attribute_by_id(self) -> pg8000.PreparedStatement:
        return self.db_conn.prepare(
            """
            SELECT text_id
            FROM attribute_text_values
            WHERE id = :id
                AND other_id = :other_id;
            """
        )

def get_text_attribute_by_id(self, id: int, other_id: int) -> int:
        if 'text_attribute_by_id' not in self.prepared_statements:
            self.prepared_statements['text_attribute_by_id'] = self.prepare_text_attribute_by_id()

        res = self.prepared_statements['text_attribute_by_id'].run(
            id=id,
            other_id=other_id,
        )

        return res[0][0] if res else None
tlocke commented 3 years ago

Hi @asvinours, thanks for the bug report. I tried to reproduce it but I couldn't. Would you be able to give a Minimum Reproducible Example? Thanks :-)

asvinours commented 3 years ago

I tried reproducing it with a dummy app but I can't. I still get the error in the full-featured app though, and it goes away if I downgrade to 1.21.x

I'll keep trying to reproduce it on a dummy app so we can figure out what is going on.

tlocke commented 2 years ago

I'll close this for now as it's been a while since it was raised, but please do re-open if you feel there's a bug in pg8000.