SeaQL / sea-orm

🐚 An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
7.3k stars 513 forks source link

empty finds return non-None #1055

Closed epipheus closed 2 years ago

epipheus commented 2 years ago

Description

This feels like a bug but could just be my lack of understanding, I'm new to sea-orm and frankly to Rust as well. I wrote a test to fetch model data from the db and instead of it returning None, it returned: Option<entities::_entity_name_::Model>::None and as a result the expect failure message doesn't trigger and it moves on. Super weird, right. To actually get the proper panick message I have to do it again.

Steps to Reproduce

  1. With an empty entity table, fetch an item like this:
    let s = Subscription::find()
        .one(&db)
        .await
        .expect("Failed to fetch subscription");
  1. Even tho it's essentially None expect won't trigger; after this line s is still an Option (why?). However, if I do this afterwards, this expect triggers
let s: subscription::Model = s.expect("Failed to fetch a saved subscription");

Am I missing something? When theres an empty fetch I should get a None right?

Expected Behavior

None being returned

Actual Behavior

Option<entities::subscription::Model>::None is returned

Reproduces How Often

Always does this for me

Versions

❯ cargo tree | grep sea-
β”‚   └── sea-orm v0.9.2
β”‚       β”œβ”€β”€ sea-orm-macros v0.9.2 (proc-macro)
β”‚       β”œβ”€β”€ sea-query v0.26.3
β”‚       β”‚   β”œβ”€β”€ sea-query-derive v0.2.0 (proc-macro)
β”‚       β”‚   β”œβ”€β”€ sea-query-driver v0.2.2 (proc-macro)
β”‚       β”œβ”€β”€ sea-strum v0.23.0
β”‚       β”‚   └── sea-strum_macros v0.23.0 (proc-macro)
β”‚   └── sea-orm-migration v0.9.2
β”‚       β”œβ”€β”€ sea-orm v0.9.2 (*)
β”‚       β”œβ”€β”€ sea-orm-cli v0.9.2
β”‚       β”‚   β”œβ”€β”€ sea-schema v0.9.3
β”‚       β”‚   β”‚   β”œβ”€β”€ sea-query v0.26.3 (*)
β”‚       β”‚   β”‚   └── sea-schema-derive v0.1.0 (proc-macro)
β”‚       β”œβ”€β”€ sea-schema v0.9.3 (*)
β”œβ”€β”€ sea-orm v0.9.2 (*)

Additional Information

postgres-db, mac os BigSur

billy1624 commented 2 years ago

Hey @epipheus, welcome!! This is the intended behaviour. Please check below for the explanation.

let s: Result<Option<subscription::Model>, DbErr> = Subscription::find()
        .one(&db)
        .await;
let s: Option<subscription::Model> = Subscription::find()
        .one(&db)
        .await?; // Note the `?` here
let s: subscription::Model = Subscription::find()
        .one(&db)
        .await? // Note the `?` here
        .expect("Failed to fetch a saved subscription"); // Unwrap the `Option<T>`
epipheus commented 2 years ago

Ah.. OK. Guess I missed this somehow. Thank you for responding. Please forgive me for cluttering your issues with this. Is there any gotcha for an async tokio::test? adding a ? yielded the following error: the trait "FromResidual<Result<Infallible, sea_orm::DbErr>>" is not implemented for "()"

billy1624 commented 2 years ago

No worries! :D