Skip to content

Unable to use query_as with Postgres enum support #313

Closed
@ecton

Description

@ecton

This example should be able to be run to create the test table, and then the unit tests will fail compilation because of the error:

the trait bound std::result::Result<std::option::Option<i32>, sqlx_core::error::Error>: sqlx::result_ext::ResultExt<std::option::Option<tests::ReprI32>> is not satisfied
the following implementations were found:
<std::result::Result<T, sqlx_core::error::Error> as sqlx::result_ext::ResultExt>
<std::result::Result<T, sqlx_core::error::Error> as sqlx::result_ext::ResultExt<std::option::Option>>
<std::result::Result<std::option::Option, sqlx_core::error::Error> as sqlx::result_ext::ResultExt>

I've also tried this with the repr(32) and the text rename functionality. I tried looking for examples in the repository, but there are no hand-written examples, just ones that use some higher-level testing functionality.

I looked for missing trait includes, but couldn't find what should be included if anything. Any help is greatly appreciated!

The cargo dependencies section for the example:

[dependencies]
sqlx = {version = "0.3", default-features=false, features=[ "runtime-tokio", "macros" , "postgres", "uuid", "chrono",  "tls" ]}
tokio = {version = "*", features = ["macros"]}
anyhow = "*"
dotenv = "*"

The main.rs file:

use sqlx::postgres::PgPool;

async fn pg() -> PgPool {
    PgPool::new("postgres://testuser:password@localhost/testdb")
        .await
        .unwrap()
}

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
    sqlx::query("CREATE TABLE testtable(intnullcolumn INT, intnotnullcolumn INT NOT NULL, textnullcolumn TEXT, textnotnullcolumn TEXT NOT NULL)").execute(&pg().await).await?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::pg;
    #[derive(PartialEq, Copy, Clone, Debug, sqlx::Type)]
    #[repr(i32)]
    enum ReprI32 {
        Test = 0,
    }

    struct ReprI32OptionRow {
        value: Option<ReprI32>,
    }

    struct ReprI32Row {
        value: ReprI32,
    }

    #[tokio::test]
    async fn null_int_cast() -> Result<(), sqlx::Error> {
        sqlx::query_as!(
            ReprI32OptionRow,
            "SELECT intnullcolumn as value FROM testtable"
        )
        .fetch_one(&pg().await)
        .await
        .map(|_| ())
    }

    #[tokio::test]
    async fn not_null_int_cast() -> Result<(), sqlx::Error> {
        sqlx::query_as!(
            ReprI32Row,
            "SELECT intnotnullcolumn as value FROM testtable"
        )
        .fetch_one(&pg().await)
        .await
        .map(|_| ())
    }

    #[tokio::test]
    async fn not_null_into_option_cast() -> Result<(), sqlx::Error> {
        sqlx::query_as!(
            ReprI32OptionRow,
            "SELECT intnotnullcolumn as value FROM testtable"
        )
        .fetch_one(&pg().await)
        .await
        .map(|_| ())
    }
}

Activity

abonander

abonander commented on May 15, 2020

@abonander
Collaborator

The macros are currently completely unaware of custom types, but we can roll this in with the following proposal: #121 (comment)

I'm starting on that right after #249 is merged as it lays some groundwork for it.

ecton

ecton commented on May 15, 2020

@ecton
Author

Awesome, thank you for the info. If you'd prefer to close this rather than keeping it open, feel free, I can follow along on the other issues.

phated

phated commented on May 18, 2020

@phated
Contributor

I just ran into this also. Excited to see how decoupled mode helps to support this!

abonander

abonander commented on May 18, 2020

@abonander
Collaborator

It doesn't directly, but the refactors I did in the course of implementing that lead directly into supporting this and #121 .

mehcode

mehcode commented on Jun 13, 2020

@mehcode
Member

Initial support for user-defined types in macros is being tracked in #397

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

        Participants

        @ecton@mehcode@phated@abonander

        Issue actions

          Unable to use query_as with Postgres enum support · Issue #313 · launchbadge/sqlx