pgvector / pgvector-python

pgvector support for Python
MIT License
979 stars 63 forks source link

SQLAlchemy does not recognize vector column #74

Closed kreleonie closed 5 months ago

kreleonie commented 5 months ago

Hello, I have an existing database with a table containing a vector column ('embeddings') When I try to access the existing database with SQLAlchemy over automap_base(), I get error messages that the vector type for the column is not recognized.

This is how I try to connect:

conn_string = f"""postgresql+psycopg2:// (...)"""
engine = create_engine(conn_string)

    with engine.connect() as conn:
        conn.execute(text('CREATE EXTENSION IF NOT EXISTS vector'))
        conn.commit()

Base = automap_base()
Base.prepare(autoload_with=engine)

session = Session(engine)
SearchTable = Base.metadata.tables[tablename]

I get the following error message:

SAWarning: Did not recognize type 'vector' of column 'embeddings'

The column is definitely a column with type vector. What is wrong in my code?

ankane commented 5 months ago

Hi @kreleonie, you need to import the Vector type.

from pgvector.sqlalchemy import Vector