{"record":{"id":"836a627864083b31","repo":"SeaQL/sea-orm","slug":"index-out-of-bounds-the-activehasmany-is-notset-836a62","errorCode":null,"errorMessage":"index out of bounds: the ActiveHasMany is NotSet (index: {index})","messagePattern":"index out of bounds: the ActiveHasMany is NotSet \\(index: (.+?)\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/entity/active_model_ex.rs","lineNumber":430,"sourceCode":"    }\n}\n\nimpl<E: EntityTrait> From<ActiveHasMany<E>> for Option<Vec<E::ActiveModelEx>> {\n    fn from(value: ActiveHasMany<E>) -> Self {\n        match value {\n            ActiveHasMany::NotSet => None,\n            ActiveHasMany::Replace(models) | ActiveHasMany::Append(models) => Some(models),\n        }\n    }\n}\n\nimpl<E: EntityTrait> Index<usize> for ActiveHasMany<E> {\n    type Output = E::ActiveModelEx;\n\n    fn index(&self, index: usize) -> &Self::Output {\n        match self {\n            ActiveHasMany::NotSet => {\n                panic!(\"index out of bounds: the ActiveHasMany is NotSet (index: {index})\")\n            }\n            ActiveHasMany::Replace(models) | ActiveHasMany::Append(models) => models.index(index),\n        }\n    }\n}\n\nimpl<E: EntityTrait> IndexMut<usize> for ActiveHasMany<E> {\n    fn index_mut(&mut self, index: usize) -> &mut Self::Output {\n        match self {\n            ActiveHasMany::NotSet => {\n                panic!(\"index out of bounds: the ActiveHasMany is NotSet (index: {index})\")\n            }\n            ActiveHasMany::Replace(models) | ActiveHasMany::Append(models) => {\n                models.index_mut(index)\n            }\n        }\n    }\n}","sourceCodeStart":412,"sourceCodeEnd":448,"githubUrl":"https://github.com/SeaQL/sea-orm/blob/e29bcd1b417c41a553b386fe94511d7c64a1c8ec/src/entity/active_model_ex.rs#L412-L448","documentation":"The `Index<usize>` implementation for `ActiveHasMany<E>` panics when the collection is `NotSet`, because there are no child models to index — the relation was never populated on this active model. `Replace`/`Append` variants delegate to the inner vector's own bounds check. The message reports the requested index.","triggerScenarios":"Evaluating `active_model.children[0]` (or any index) when the has-many relation was never set via `set_children`/`Replace`/`Append` after building the active model.","commonSituations":"Loading an entity without `.with(child::Entity)` and then indexing its relation; assuming relations come pre-populated from a find query; constructing a fresh ActiveModel and reading children before pushing any.","solutions":["Load the relation first: `Entity::load().with(child::Entity)` or call the relation setter before indexing.","Check `matches!(am.children, ActiveHasMany::NotSet)` (or an `is_not_set`/`is_set` helper) before indexing.","Use the collection's accessor/iterator methods that return `Option` instead of `Index`.","Initialize children with `Replace(vec![])` if an empty collection is valid for your flow."],"exampleFix":"// before\nlet first = &user.posts[0]; // panics if posts NotSet\n\n// after\nif !user.posts.is_not_set() {\n    let first = &user.posts[0];\n}","handlingStrategy":"validation","validationCode":"let first = match &user.posts {\n    ActiveHasMany::NotSet => return Err(anyhow!(\"posts not loaded\")),\n    ActiveHasMany::Replace(m) | ActiveHasMany::Append(m) => m.first(),\n};","typeGuard":"fn posts_loaded<E: EntityTrait>(posts: &ActiveHasMany<E>) -> bool {\n    !matches!(posts, ActiveHasMany::NotSet)\n}","tryCatchPattern":"std::panic::catch_unwind(|| user.posts[0].clone()) // prefer the match/guard above","preventionTips":["Load relations with .with(child::Entity) before indexing.","Treat NotSet as 'not loaded', not 'empty' — check before indexing.","Prefer .first()/.get(i) style accessors returning Option.","Initialize relations with Replace(vec![]) when an empty set is valid."],"tags":["panic","index-out-of-bounds","relation","has-many"],"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"}