{"record":{"id":"1e9037bcd3caa6fb","repo":"SeaQL/sea-orm","slug":"called-belongsto-unwrap-on-an-unloaded-valu-1e9037","errorCode":null,"errorMessage":"called `BelongsTo::unwrap()` on an `Unloaded` value","messagePattern":"called `BelongsTo::unwrap\\(\\)` on an `Unloaded` value","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/entity/compound/belongs_to.rs","lineNumber":137,"sourceCode":"            Self::Unloaded => None,\n        }\n    }\n\n    /// Convert into an `Option<ModelEx>`\n    pub fn into_option(self) -> Option<E::ModelEx> {\n        match self {\n            Self::Loaded(model) => Some(*model),\n            Self::Unloaded => None,\n        }\n    }\n\n    /// # Panics\n    ///\n    /// Panics if called on an `Unloaded` value.\n    pub fn unwrap(self) -> E::ModelEx {\n        match self {\n            Self::Loaded(model) => *model,\n            Self::Unloaded => panic!(\"called `BelongsTo::unwrap()` on an `Unloaded` value\"),\n        }\n    }\n}\n\nimpl<E> BelongsTo<Option<E>>\nwhere\n    E: EntityTrait,\n{\n    /// Return true if this optional relation was loaded and no model was found.\n    pub fn is_not_found(&self) -> bool {\n        matches!(self, Self::Loaded(None))\n    }\n\n    /// Return true if this optional relation holds no model — i.e. it is either\n    /// `Unloaded` or was loaded with no match (`is_not_found`).\n    pub fn is_unloaded_or_not_found(&self) -> bool {\n        matches!(self, Self::Unloaded | Self::Loaded(None))\n    }","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/SeaQL/sea-orm/blob/e29bcd1b417c41a553b386fe94511d7c64a1c8ec/src/entity/compound/belongs_to.rs#L119-L155","documentation":"BelongsTo<E>::unwrap() returns the related model only when the relation is Loaded; it panics here because the variant is Unloaded — the belonging relation was never fetched (no .with(...)/find_related load ran before unwrap), so no model exists to move out. This flags an API-usage ordering mistake, not a missing database row.","triggerScenarios":"Calling .unwrap() on a BelongsTo<E> field of a model whose relation was not loaded via .with(...) / load() before access.","commonSituations":"Fetching an entity without eager-loading the belongs_to relation then immediately reading the field; forgetting to include the relation in the .with() chain; loading failing silently.","solutions":["Eager-load the relation: use .with(rel::Entity) on the query","Call .load(db).await? on the BelongsTo before unwrap()","Use try_get() / is_loaded() checks instead of unwrap()"],"exampleFix":"// before\nlet parent = user.company.unwrap();\n// after\nlet user = user::Entity::load().filter_by_id(id).with(company::Entity).one(db).await?;\nlet parent = user.company.unwrap();","handlingStrategy":"validation","validationCode":"if rel.is_loaded() { /* safe to access */ } else { /* load first */ }","typeGuard":"fn loaded<E>(b: &BelongsTo<E>) -> Option<&E::ModelEx> { b.as_loaded() }","tryCatchPattern":"let parent = company.load(db).await?.unwrap();","preventionTips":["Always include belongs_to relations in .with(...) chains","Check is_loaded()/try accessors before unwrap","Centralize entity-loading helpers that eager-load the standard set of relations"],"tags":["rust","panic","orm","lazy-loading","relations"],"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"}