sfackler / rust-postgres

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

Postgres REGCLASS is not convertible to rust native Strings #1135

Closed uds5501 closed 4 months ago

uds5501 commented 7 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 7 months ago

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

sfackler commented 4 months ago

See #1041