sfackler / rust-postgres

Native PostgreSQL driver for the Rust programming language
Apache License 2.0
3.39k stars 429 forks source link

Postgres REGCLASS is not convertible to rust native Strings #1135

Open uds5501 opened 2 months ago

uds5501 commented 2 months ago

Overview:

It seems that postgres "regclass" is not convertible to native rust strings.

Triage:

I was trying to fetch an array of columns for a table where primary keys are indexed using the following snippet:

    // ... some function above
    let table_indexed_attributes_query = r#"
        SELECT array_agg(a.attname)
        FROM   pg_index i
        JOIN   pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)
        WHERE  i.indrelid = $1::regclass
        AND    i.indisprimary;
    "#;

    let result_indexed_attributes = client.borrow_mut().query(table_indexed_attributes_query, &[&table_name]).unwrap();
    let indexed_attributes = match result_indexed_attributes.get(0) {
        Some(indexed_attributes) => {
            let data: String = indexed_attributes.get(0);
            let indexed_columns = data.split(",")
                .map(|s| s.to_string())
                .collect();
            indexed_columns
        }
        None => vec![],
    };

While running, I am getting the following error:

called `Result::unwrap()` on an `Err` value: Error { kind: ToSql(0), cause: Some(WrongType { postgres: Regclass, rust: "alloc::string::String" }) }
image

Question:

While I can't convert regclass to String, is there any rust native object I could convert it into? If not, I'd like to help build support for this.

uds5501 commented 2 months ago

Apologies: I noticed that the issue not the conversation from regclass to String, rather it's the other way around.