{"record":{"id":"381f9f71dee96237","repo":"SeaQL/sea-orm","slug":"index-out-of-bounds-the-hasmany-is-unloaded-inde-381f9f","errorCode":null,"errorMessage":"index out of bounds: the HasMany is Unloaded (index: {index})","messagePattern":"index out of bounds: the HasMany is Unloaded \\(index: (.+?)\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/entity/compound/has_many.rs","lineNumber":102,"sourceCode":"    }\n}\n\nimpl<E: EntityTrait> From<HasMany<E>> for Option<Vec<E::ModelEx>> {\n    fn from(value: HasMany<E>) -> Self {\n        match value {\n            HasMany::Loaded(models) => Some(models),\n            HasMany::Unloaded => None,\n        }\n    }\n}\n\nimpl<E: EntityTrait> Index<usize> for HasMany<E> {\n    type Output = E::ModelEx;\n\n    fn index(&self, index: usize) -> &Self::Output {\n        match self {\n            HasMany::Unloaded => {\n                panic!(\"index out of bounds: the HasMany is Unloaded (index: {index})\")\n            }\n            HasMany::Loaded(items) => items.index(index),\n        }\n    }\n}\n\nimpl<E: EntityTrait> IntoIterator for HasMany<E> {\n    type Item = E::ModelEx;\n    type IntoIter = std::vec::IntoIter<E::ModelEx>;\n\n    fn into_iter(self) -> Self::IntoIter {\n        match self {\n            HasMany::Loaded(models) => models.into_iter(),\n            HasMany::Unloaded => Vec::new().into_iter(),\n        }\n    }\n}\n","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/SeaQL/sea-orm/blob/e29bcd1b417c41a553b386fe94511d7c64a1c8ec/src/entity/compound/has_many.rs#L84-L120","documentation":"Indexing a HasMany<E> with [] requires the relation to be loaded and the index within range. If the HasMany is Unloaded (never fetched) there is no backing vector, so indexing panics with this message.","triggerScenarios":"Using my_model.items[0] (Index<usize>) on a has_many relation that was never loaded via .with(...) or .load().","commonSituations":"Accessing child collections without eager loading; forgetting the relation in the .with() chain; calling index before an awaited load completes.","solutions":["Eager-load with .with(items::Entity) on the query","Call .load(db).await? on the HasMany before indexing","Use .as_ref() / try accessors to get Option<&[Model]> and check bounds"],"exampleFix":"// before\nlet first = user.posts[0];\n// after\nlet posts = user.posts.try_get().unwrap_or_default();\nif let Some(first) = posts.first() { /* ... */ }","handlingStrategy":"validation","validationCode":"if !user.posts.is_loaded() { return Err(\"posts not loaded\"); }","typeGuard":"fn loaded_items<E: EntityTrait>(h: &HasMany<E>) -> &[E::ModelEx] { h.as_loaded().unwrap_or(&[]) }","tryCatchPattern":"let items = user.posts.load(db).await?; let first = items.first().ok_or_else(|| anyhow!(\"no posts\"))?;","preventionTips":["Eager-load has_many collections via .with(...) before indexing","Prefer .first()/.get(i) with Option handling over direct [] indexing","Check is_loaded() before any indexed access"],"tags":["rust","panic","orm","index-out-of-bounds","relations"],"backgroundTag":"index-out-of-bounds","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"}