Fix syntax error where the previous use of :metadata::text in some sqlachamy/postgres versions doesn't bind at all

Fix syntax error where the previous use of :metadata::text in some sqlachamy/postgres versions doesn't bind the variable at all
This commit is contained in:
psy42a 2025-08-05 23:27:50 +10:00 committed by GitHub
parent 66341124a6
commit f3b0f7d358
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -203,6 +203,8 @@ class PgvectorClient(VectorDBBase):
for item in items: for item in items:
vector = self.adjust_vector_length(item["vector"]) vector = self.adjust_vector_length(item["vector"])
# Use raw SQL for BYTEA/pgcrypto # Use raw SQL for BYTEA/pgcrypto
# Ensure metadata is converted to its JSON text representation
json_metadata = json.dumps(item["metadata"])
self.session.execute( self.session.execute(
text( text(
""" """
@ -211,7 +213,7 @@ class PgvectorClient(VectorDBBase):
VALUES ( VALUES (
:id, :vector, :collection_name, :id, :vector, :collection_name,
pgp_sym_encrypt(:text, :key), pgp_sym_encrypt(:text, :key),
pgp_sym_encrypt(:metadata::text, :key) pgp_sym_encrypt(:metadata_text, :key)
) )
ON CONFLICT (id) DO NOTHING ON CONFLICT (id) DO NOTHING
""" """
@ -221,7 +223,7 @@ class PgvectorClient(VectorDBBase):
"vector": vector, "vector": vector,
"collection_name": collection_name, "collection_name": collection_name,
"text": item["text"], "text": item["text"],
"metadata": json.dumps(item["metadata"]), "metadata_text": json_metadata,
"key": PGVECTOR_PGCRYPTO_KEY, "key": PGVECTOR_PGCRYPTO_KEY,
}, },
) )
@ -255,6 +257,7 @@ class PgvectorClient(VectorDBBase):
if PGVECTOR_PGCRYPTO: if PGVECTOR_PGCRYPTO:
for item in items: for item in items:
vector = self.adjust_vector_length(item["vector"]) vector = self.adjust_vector_length(item["vector"])
json_metadata = json.dumps(item["metadata"])
self.session.execute( self.session.execute(
text( text(
""" """
@ -263,7 +266,7 @@ class PgvectorClient(VectorDBBase):
VALUES ( VALUES (
:id, :vector, :collection_name, :id, :vector, :collection_name,
pgp_sym_encrypt(:text, :key), pgp_sym_encrypt(:text, :key),
pgp_sym_encrypt(:metadata::text, :key) pgp_sym_encrypt(:metadata_text, :key)
) )
ON CONFLICT (id) DO UPDATE SET ON CONFLICT (id) DO UPDATE SET
vector = EXCLUDED.vector, vector = EXCLUDED.vector,
@ -277,7 +280,7 @@ class PgvectorClient(VectorDBBase):
"vector": vector, "vector": vector,
"collection_name": collection_name, "collection_name": collection_name,
"text": item["text"], "text": item["text"],
"metadata": json.dumps(item["metadata"]), "metadata_text": json_metadata,
"key": PGVECTOR_PGCRYPTO_KEY, "key": PGVECTOR_PGCRYPTO_KEY,
}, },
) )