{"record":{"id":"7fe425938f2ef255","repo":"SeaQL/sea-orm","slug":"called-hasone-unwrap-on-a-loaded-none-valu-7fe425","errorCode":null,"errorMessage":"called `HasOne::unwrap()` on a `Loaded(None)` value","messagePattern":"called `HasOne::unwrap\\(\\)` on a `Loaded\\(None\\)` value","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/entity/compound/has_one.rs","lineNumber":78,"sourceCode":"        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\nimpl<E: EntityTrait> From<HasOne<E>> for Option<Box<E::ModelEx>> {","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/SeaQL/sea-orm/blob/e29bcd1b417c41a553b386fe94511d7c64a1c8ec/src/entity/compound/has_one.rs#L60-L96","documentation":"HasOne<E>::unwrap() was called on a Loaded(None) value: the relation was loaded and the query succeeded, but no related row matched (dangling foreign key or deleted target). unwrap() intentionally panics on an absent related row since 'none found' is a normal result callers should handle with into_option()/take() or explicit matching.","triggerScenarios":"Calling .unwrap() on a loaded optional has_one whose related row does not exist — user has no profile row yet.","commonSituations":"One-to-one relations that are optional by schema; the child row was deleted; insert order mistakes where the child row was never created.","solutions":["Handle the None case: if let Some(p) = loaded relation value","Create the missing related row before unwrap, e.g. insert the profile","Use try_unwrap() to handle missing relations gracefully"],"exampleFix":"// before\nlet profile = user.profile.unwrap();\n// after\nlet profile = match user.profile.try_unwrap()? { Some(p) => p, None => create_default_profile(user).await? };","handlingStrategy":"fallback","validationCode":"if matches!(user.profile, HasOne::Loaded(None)) { /* create or default */ }","typeGuard":"fn has_profile<E>(h: &HasOne<Option<E>>) -> bool { matches!(h, HasOne::Loaded(Some(_))) }","tryCatchPattern":"let profile = user.profile.try_unwrap()?.unwrap_or_else(default_profile);","preventionTips":["Handle the no-related-row case for optional one-to-one relations","Create dependent rows (e.g. profile) in the same transaction as the parent","Avoid unwrap on Option-typed relations"],"tags":["rust","panic","orm","nullable","relations"],"backgroundTag":"null-argument","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"}