{"record":{"id":"2938120f3a5a4669","repo":"SeaQL/sea-orm","slug":"called-belongsto-unwrap-on-an-unloaded-valu","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":"sea-orm-sync/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/sea-orm-sync/src/entity/compound/belongs_to.rs#L119-L155","documentation":"`BelongsTo::unwrap` on the non-optional variant returns the loaded parent model but panics when the relation is `Unloaded`, i.e. the belongs-to relation was never fetched. Like `Option::unwrap`, it is intended only after confirming the relation is `Loaded`.","triggerScenarios":"Calling `child.parent.unwrap()` where `parent` is a `BelongsTo` field still in `Unloaded` state — the `with(parent::Entity)` load was skipped, or the model was built/deserialized without loading the relation.","commonSituations":"Forgetting `.with(...)` in a loader query then accessing the relation; models constructed in tests without loading relations; relation lost after mapping the model to another shape.","solutions":["Load the relation before unwrapping: `child::Entity::load().filter_by_id(id).with(parent::Entity).one(db)`.","Match on `BelongsTo::Loaded(model)` / `Unloaded` and handle the unloaded case instead of panicking.","Use a fallible accessor (e.g. `try_...` or `as_loaded()`) if available and return an error.","Ensure the code path that loads parents isn't conditionally skipped."],"exampleFix":"// before\nlet parent = post.author.unwrap(); // panics if unloaded\n// after\nlet post = post::Entity::load().filter_by_id(id).with(user::Entity).one(db)?.unwrap();\nlet parent = post.author.unwrap();","handlingStrategy":"validation","validationCode":"// Load the relation before unwrapping\nlet post = post::Entity::load().filter_by_id(id).with(user::Entity).one(db)?.unwrap();\nassert!(matches!(post.author, BelongsTo::Loaded(_)), \"author relation must be loaded\");","typeGuard":"fn loaded_parent<E>(b: &BelongsTo<E>) -> Option<&E::ModelEx> {\n    match b { BelongsTo::Loaded(m) => Some(m), BelongsTo::Unloaded => None }\n}","tryCatchPattern":"// Avoid panic-catching; check state first\nif let Some(parent) = loaded_parent(&post.author) { /* use parent */ } else { /* load it */ }","preventionTips":["Always call .with(parent::Entity) in loader queries for relations you will read","Treat relation fields as Unloaded until proven loaded","In fixtures/tests, load or explicitly set relations before access"],"tags":["panic","relation","belongs-to","unloaded","lazy-loading"],"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"}