{"record":{"id":"85a17961d70af8dd","repo":"SeaQL/sea-orm","slug":"called-belongsto-unwrap-on-a-loaded-none-v","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":"error","filePath":"sea-orm-sync/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/sea-orm-sync/src/entity/compound/belongs_to.rs#L170-L206","documentation":"Same optional belongs-to `unwrap` as error 17, but this panic fires when the relation IS loaded yet holds `Loaded(None)` — the foreign key is NULL or no matching parent row exists, so unwrapping an optional parent that is absent fails.","triggerScenarios":"Calling `child.parent.unwrap()` where `parent` was fetched via `.with(parent::Entity)` but the row's foreign key is NULL or points to a missing parent, yielding `Loaded(None)`.","commonSituations":"Nullable foreign key columns treated as mandatory; orphaned rows after the parent was deleted without cascading; data imported with dangling references.","solutions":["Handle the None case: match `BelongsTo::Loaded(Some(p))` and treat absent parents explicitly.","If the relation should always exist, make the foreign key NOT NULL at the schema level and fix orphaned data.","Use `unwrap_or_default`/return an `Option` to the caller instead of panicking.","Add a cascade delete or cleanup job to prevent orphan rows."],"exampleFix":"// before\nlet parent = comment.post.unwrap(); // panics on Loaded(None)\n// after\nlet parent = match comment.post {\n    BelongsTo::Loaded(Some(p)) => Some(p),\n    _ => None, // nullable FK: parent may legitimately be absent\n};","handlingStrategy":"fallback","validationCode":"// Treat Loaded(None) as a legitimate outcome for nullable FKs\nlet parent = match comment.post {\n    BelongsTo::Loaded(Some(p)) => Some(p),\n    _ => None,\n};","typeGuard":"fn has_parent<E>(b: &BelongsTo<Option<E>>) -> bool {\n    matches!(b, BelongsTo::Loaded(Some(_)))\n}","tryCatchPattern":"// Use fallback semantics instead of unwrap\nlet parent = opt_parent(&comment.post).ok_or(MyError::ParentMissing)?;","preventionTips":["Never unwrap optional belongs-to relations backed by nullable FKs","Enforce NOT NULL + cascade deletes when the parent is semantically required","Clean up orphaned rows referencing deleted parents"],"tags":["panic","relation","belongs-to","nullable","foreign-key"],"backgroundTag":"entity-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"}