{"record":{"id":"ab56b204962e749d","repo":"SeaQL/sea-orm","slug":"called-belongsto-unwrap-on-a-loaded-none-v-ab56b2","errorCode":null,"errorMessage":"called `BelongsTo::unwrap()` on a `Loaded(None)` value","messagePattern":"called `BelongsTo::unwrap\\(\\)` on a `Loaded\\(None\\)` value","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/entity/compound/belongs_to.rs","lineNumber":188,"sourceCode":"        }\n    }\n\n    /// Convert into an `Option<ModelEx>`\n    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    /// # 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 `BelongsTo::unwrap()` on an `Unloaded` value\"),\n            Self::Loaded(None) => panic!(\"called `BelongsTo::unwrap()` on a `Loaded(None)` value\"),\n        }\n    }\n}\n\nimpl<E> BelongsTo<E>\nwhere\n    E: EntityTrait,\n    E::ActiveModelEx: From<E::ModelEx>,\n{\n    pub fn into_active_model(self) -> ActiveBelongsTo<E> {\n        match self {\n            Self::Loaded(model) => ActiveBelongsTo::set(*model),\n            Self::Unloaded => ActiveBelongsTo::NotSet,\n        }\n    }\n}\n\nimpl<E> BelongsTo<Option<E>>","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/SeaQL/sea-orm/blob/e29bcd1b417c41a553b386fe94511d7c64a1c8ec/src/entity/compound/belongs_to.rs#L170-L206","documentation":"BelongsTo<Option<E>>::unwrap() was called on a Loaded(None) value: the relation was loaded but no related parent row exists (dangling/deleted foreign key). unwrap() refuses to fabricate a model for an absent parent, so it panics; absence of the parent is a legitimate state that callers must handle rather than force-open.","triggerScenarios":"Calling .unwrap() on a loaded optional belongs_to whose foreign key is NULL or points to a deleted row (loaded to None).","commonSituations":"Nullable foreign keys where the row legitimately has no parent; the parent row was deleted after insert; NOT NULL assumption broken by schema change.","solutions":["Match on the Option instead of unwrapping: if let Some(parent) = relation value","Use try_unwrap() and handle the missing-relation case","Make the foreign key NOT NULL in the schema if a parent is truly required"],"exampleFix":"// before\nlet parent = post.author.unwrap();\n// after\nlet parent = post.author.try_unwrap()?.unwrap_or_default_parent();","handlingStrategy":"fallback","validationCode":"if matches!(post.author, BelongsTo::Loaded(None)) { /* no parent — handle */ }","typeGuard":"fn has_parent<E>(b: &BelongsTo<Option<E>>) -> bool { matches!(b, BelongsTo::Loaded(Some(_))) }","tryCatchPattern":"let parent = post.author.try_unwrap()?.unwrap_or_default();","preventionTips":["Treat Loaded(None) as a valid state for nullable foreign keys","Use Option-aware accessors instead of unwrap on optional relations","Enforce NOT NULL foreign keys in schema when a parent is mandatory"],"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"}