Skip to content

query_as!() gives an error for a non-existing column for an expression #3806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zbrox opened this issue Mar 25, 2025 · 5 comments
Open

query_as!() gives an error for a non-existing column for an expression #3806

zbrox opened this issue Mar 25, 2025 · 5 comments
Labels

Comments

@zbrox
Copy link

zbrox commented Mar 25, 2025

I have found these related issues/pull requests

I haven't found similar issues

Description

Hey,

I've been using the query_as! macro with an expression, doing a similarity search with pgvector.

Here's an example:

CREATE TABLE vectors (
    id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
    embedding vector(3072) NOT NULL,
    metadata jsonb NOT NULL DEFAULT '{}'::jsonb,
    text text NOT NULL,
    collection TEXT NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct SearchResult {
    pub id: uuid::Uuid,
    pub metadata: serde_json::Value,
    pub document_id: uuid::Uuid,
    pub text: String,
    pub score: f64,
}

sqlx::query_as!(
    SearchResult,
    r#"SELECT id as "id!:_", text, metadata as "metadata!:_", (1 - (embedding <=> $1)) as "score!:_"
            FROM vectors
            WHERE collection = $2
            ORDER BY score DESC"#,
    vector as pgvector::Vector,
    collection,
)
.fetch_all(&mut *conn)
.await

With this I get the error column "score" does not exist. If I remove the type casting and just specify it as "score" then I get an error because it cannot convert it from an Option.

Reproduction steps

The minimal example above should reproduce the error.

I'm using pgvector 0.4.0.

SQLx version

0.8.3

Enabled SQLx features

"runtime-tokio", "chrono", "uuid", "postgres", "migrate", "derive"

Database server and version

Postgres 17

Operating system

macos 15.3.2

Rust version

1.85.1

@zbrox zbrox added the bug label Mar 25, 2025
@abonander
Copy link
Collaborator

The minimal example above should reproduce the error.

@zbrox that is not a sufficiently minimal example. There's no table schema, nor a definition for SearchResult. It should not require guessing what the rest of your code looks like.

@zbrox
Copy link
Author

zbrox commented Apr 16, 2025

The minimal example above should reproduce the error.

@zbrox that is not a sufficiently minimal example. There's no table schema, nor a definition for SearchResult. It should not require guessing what the rest of your code looks like.

You are right, I'm sorry, I have omitted this in haste. I will edit the report.

@xpe
Copy link
Contributor

xpe commented Apr 20, 2025

@zbrox Any updates? I'm also working through some hiccups with pgvector and pgvector-rust. I'll try to take a look. Maybe we can help each other out.

@zbrox
Copy link
Author

zbrox commented Apr 22, 2025

@xpe No, nothing new. To work around it quickly I just use it as an Option and then force unwrap it with an expect, knowing that it will always have a value. Not great, but for now it works. I had to move on quickly from this, I didn't have much time unfortunately.

@xpe
Copy link
Contributor

xpe commented Apr 27, 2025

@zbrox You probably already know this, but just in case you don't: there is a lot of detailed documentation about SQLx to be found around option handling. For example:

https://docs.rs/sqlx/latest/sqlx/macro.query.html#overrides-cheatsheet

Image

I refer to that that doc.rs page frequently when I find myself in trouble.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants