How to add custom postgres types (e.g. pgvector) in SQLAlchemy + Alembic? #1324
-
When I describe a table model like from pgvector.sqlalchemy import Vector
import numpy as np
from sqlalchemy.orm import Mapped, mapped_column
N_DIM = 1536
class Base(DeclarativeBase):
pass
class Embedding(Base):
__tablename__ = "embeddings"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
embedding: Mapped[np.array] = mapped_column(Vector(N_DIM)) I can successfully do
After digging in the code for some time, I found that the reason is this line here: # venv/Lib/site-packages/sqlalchemy/dialects/postgresql/base.py:3776
coltype = None Looking into the code in # attype == 'vector' here
if attype in self.ischema_names:
coltype = self.ischema_names[attype]
break So my workaround in def do_run_migrations(connection: Connection) -> None:
# Need to hack the "vector" type into postgres dialect schema types.
# Otherwise, `alembic check` does not recognize the type
connection.dialect.ischema_names['vector'] = project.database.migration_types.Vector
context.configure(
connection=connection,
target_metadata=target_metadata,
user_module_prefix="project.database.migration_types.",
)
with context.begin_transaction():
context.run_migrations() My question is: Is there a "proper" way to do this? It's possible I missed one of the documentation pages, if so please just give me a link, I'll read up. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
SQLAlchemy itself will not know how to reflect this type, which is the warning you see. There is an event hook called column_reflect, which would be a great place to allow interception of this "vector" name, however, that event hook will give you a We dont otherwise have a hook for custom types so the way you're placing it into ischema_names right now is the only way to do it. |
Beta Was this translation helpful? Give feedback.
-
For anyone wondering, I think I've found a simpler solution based on @MIrinkov one. There's no need to add a custom
Then modify your run_migrations_online to call do_run_migrations. Also, don't forget the import in
|
Beta Was this translation helpful? Give feedback.
SQLAlchemy itself will not know how to reflect this type, which is the warning you see. There is an event hook called column_reflect, which would be a great place to allow interception of this "vector" name, however, that event hook will give you a
NullType
and you won't have the information you need (seems like that might be something that can be improved; we can add a field with the raw type reflect information so that third parties can replace theNullType
as needed).We dont otherwise have a hook for custom types so the way you're placing it into ischema_names right now is the only way to do it.