{"record":{"id":"1a889b6aeeb003d7","repo":"SeaQL/sea-orm","slug":"called-hasone-unwrap-on-an-unloaded-value","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":"sea-orm-sync/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/sea-orm-sync/src/entity/compound/has_one.rs#L59-L95","documentation":"unwrap() on HasOne<E> only works when the relation was eagerly loaded and a row was found. It fires here because the variant is Unloaded, meaning the relation was never fetched (no .with(...)/.find_also_related(...) was used before accessing it), so there is no model to return. This is a programming/API-usage error, not a data error.","triggerScenarios":"Thrown at sea-orm-sync/src/entity/compound/has_one.rs:77 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Eagerly load the relation before unwrapping, e.g. Entity::load().with(parent::Entity).one(db) so the HasOne becomes Loaded(Some(..)).","Match on the HasOne value and handle Unloaded and Loaded(None) explicitly instead of unwrapping.","Use into_option() or take(), which return None for Unloaded/Loaded(None) instead of panicking."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e29bcd1b417c41a553b386fe94511d7c64a1c8ec","analyzedAt":"2026-09-10T11:31:52.468Z","contentChangedAt":"2026-09-10T11:31:52.468Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}