{"record":{"id":"8ae04d353637cfdb","repo":"SeaQL/sea-orm","slug":"called-hasone-unwrap-on-an-unloaded-value-8ae04d","errorCode":null,"errorMessage":"called `HasOne::unwrap()` on an `Unloaded` value","messagePattern":"called `HasOne::unwrap\\(\\)` on an `Unloaded` value","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/entity/compound/has_one.rs","lineNumber":77,"sourceCode":"    pub fn into_option(self) -> Option<E::ModelEx> {\n        match self {\n            Self::Loaded(Some(model)) => Some(*model),\n            Self::Unloaded | Self::Loaded(None) => None,\n        }\n    }\n\n    /// Take ownership of the contained Model, if loaded, leaving `Unloaded` in place.\n    pub fn take(&mut self) -> Option<E::ModelEx> {\n        std::mem::take(self).into_option()\n    }\n\n    /// # Panics\n    ///\n    /// Panics if called on an `Unloaded` or `Loaded(None)` value.\n    pub fn unwrap(self) -> E::ModelEx {\n        match self {\n            Self::Loaded(Some(model)) => *model,\n            Self::Unloaded => panic!(\"called `HasOne::unwrap()` on an `Unloaded` value\"),\n            Self::Loaded(None) => panic!(\"called `HasOne::unwrap()` on a `Loaded(None)` value\"),\n        }\n    }\n}\n\nimpl<E> HasOne<E>\nwhere\n    E: EntityTrait,\n    E::ActiveModelEx: From<E::ModelEx>,\n{\n    pub fn into_active_model(self) -> ActiveHasOne<E> {\n        match self {\n            Self::Loaded(Some(model)) => ActiveHasOne::set(Some(*model)),\n            Self::Unloaded | Self::Loaded(None) => ActiveHasOne::NotSet,\n        }\n    }\n}\n","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/SeaQL/sea-orm/blob/e29bcd1b417c41a553b386fe94511d7c64a1c8ec/src/entity/compound/has_one.rs#L59-L95","documentation":"HasOne<E>::unwrap() panics because the variant is Unloaded: the has-one relation was never eagerly loaded (no .with(...) on the query before unwrap), so there is no contained model. It indicates the relation access happened before/without loading, not that the related row is missing.","triggerScenarios":"Calling .unwrap() on a has_one relation field that was not included in the .with() chain of the loading query.","commonSituations":"Fetching a user without its profile relation then calling profile.unwrap(); partial eager-load lists; refactors that drop a .with() call.","solutions":["Eager-load with .with(profile::Entity) in the query","Call .load(db).await? on the HasOne before unwrap()","Use try_unwrap() / is_loaded() checks instead of unwrap()"],"exampleFix":"// before\nlet profile = user.profile.unwrap();\n// after\nlet user = user::Entity::load().filter_by_id(id).with(profile::Entity).one(db).await?;\nlet profile = user.profile.unwrap();","handlingStrategy":"validation","validationCode":"if !user.profile.is_loaded() { return Err(\"profile not loaded\"); }","typeGuard":"fn loaded<E>(h: &HasOne<E>) -> Option<&E::ModelEx> { h.as_loaded() }","tryCatchPattern":"let profile = user.profile.load(db).await?.unwrap();","preventionTips":["Include has_one relations in the .with() chain of load queries","Use try_unwrap/is_loaded before unwrap","Keep a shared query helper that loads all commonly used relations"],"tags":["rust","panic","orm","relations","eager-loading"],"backgroundTag":"record-not-found","analyzedSha":"e29bcd1b417c41a553b386fe94511d7c64a1c8ec","analyzedAt":"2026-09-10T11:31:52.468Z","contentChangedAt":"2026-09-10T11:31:52.468Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}