Skip to content

sqlalchemy support: select on the right-hand side of <-> operator #147

Description

@javaccar

Here's an example from the pgvector docs

SELECT * FROM items WHERE id != 1 ORDER BY embedding <-> (SELECT embedding FROM items WHERE id = 1) LIMIT 5;

Notice how the right-hand side of the <-> is a select statement that should return only one value.

The pgvector-python sqlalchemy examples and test cases only ever seem to use a hardcoded embedding like [1, 1, 1] on the right-hand side.

Can it be made explicit in the documentation if a select like (SELECT embedding FROM items WHERE id = 1) can be used as an argument to the cosine_distance method, for example? If right-hand side can be a select, an example query would be helpful. If the right-hand side cannot be a select, I think that should be highlighted as a gap in the sqlalchemy support

Activity

  1. ankane commented on Jan 22, 2026

    @ankane
    Member

    Hi @javaccar, this is already supported with standard SQLAlchemy syntax. Added a test case in the commit above.

  2. javaccar commented on Jan 22, 2026

    @javaccar
    Author

    ah yes I was just about to comment and mark this as closed but you beat me by a few minutes! scalar_subquery was the key

  3. added a commit that references this issue on May 13, 2026
    8bd3c61
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions