pythonpostgresqlflaskflask-migratepgvector

flask-migrate not importing needed library pgvector


I receive a NameError for pgvector while attempting to create vector embeddings using pgvector with flask and flask-sqlalchemy.

NameError: name 'pgvector' is not defined

#extensions.py

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

#models.py

from extensions import db
from sqlalchemy.orm import mapped_column
from pgvector.sqlalchemy import Vector

N_DIM = 1536

class InstagramPosts(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    embedding = mapped_column(Vector(N_DIM))

I then migrate to my postgres db via "flask db migrate -m "added embedding" And attempt to run an upgrade via "flask db upgrade"

after which I get the name error.

[stacktrace]

"~/migrations/versions/2b0a38ba6267_added_pgvector_column.py", line 23, in upgrade
    batch_op.add_column(sa.Column('embedding', pgvector.sqlalchemy.vector.VECTOR(dim=1536), nullable=True))
                                               ^^^^^^^^
NameError: name 'pgvector' is not defined

I have created the extension in the postgres database via

CREATE EXTENSION IF NOT EXISTS vector;

And I have installed pgvector via pip install pgvector and it is visible in my installed libraries via pip freeze

Not certain it is relevant but for what it is worth, my application is a flask application with blueprints and I am using the application factory method to create the app etc.

edit ----------------

On further investigation it appears the pgvector is not automatically imported into the migrations file as it should be. I have added the migrations file below. It works if I manually open the migrations file and add the 'import pgvector'. So my question is now why pgvector is not being imported with flask-migrate as it should be.

#migration file 85a0af23e160_added_embeddings_column.py

"""added embeddings column

Revision ID: 85a0af23e160
Revises: f768e743870b
Create Date: 2024-08-15 19:39:52.497427

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '85a0af23e160'
down_revision = 'f768e743870b'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('instagram_posts', schema=None) as batch_op:
        batch_op.add_column(sa.Column('embedding', pgvector.sqlalchemy.vector.VECTOR(dim=1536), nullable=True))

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('instagram_posts', schema=None) as batch_op:
        batch_op.drop_column('embedding')

    # ### end Alembic commands ###

Solution

  • Ok. Finally figured this out. Solution: edit the migrations script.py.mako file to include any necessary imports.

    #script.py.mako

    """${message}
    
    Revision ID: ${up_revision}
    Revises: ${down_revision | comma,n}
    Create Date: ${create_date}
    
    """
    from alembic import op
    import sqlalchemy as sa
    import pgvector     #  <---- This line was added
    ${imports if imports else ""}
    
    # revision identifiers, used by Alembic.
    revision = ${repr(up_revision)}
    down_revision = ${repr(down_revision)}
    branch_labels = ${repr(branch_labels)}
    depends_on = ${repr(depends_on)}
    
    
    def upgrade():
        ${upgrades if upgrades else "pass"}
    
    
    def downgrade():
        ${downgrades if downgrades else "pass"}