{"record":{"id":"876ac6a538ac91a9","repo":"SeaQL/sea-orm","slug":"must-be-valid","errorCode":null,"errorMessage":"Must be valid","messagePattern":"Must be valid","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sea-orm-sync/src/entity/base_entity.rs","lineNumber":1063,"sourceCode":"    /// #\n    /// # Ok(())\n    /// # }\n    /// ```\n    fn delete_by_id<T>(values: T) -> ValidatedDeleteOne<Self>\n    where\n        T: Into<<Self::PrimaryKey as PrimaryKeyTrait>::ValueType>,\n    {\n        let mut am = Self::ActiveModel::default();\n        let mut keys = Self::PrimaryKey::iter();\n        for v in values.into().into_value_tuple() {\n            if let Some(key) = keys.next() {\n                let col = key.into_column();\n                am.set(col, v);\n            } else {\n                unreachable!(\"primary key arity mismatch\");\n            }\n        }\n        Delete::one(am).validate().expect(\"Must be valid\")\n    }\n}\n\n#[cfg(test)]\nmod tests {\n    #[test]\n    fn test_delete_by_id_1() {\n        use crate::tests_cfg::cake;\n        use crate::{DbBackend, entity::*, query::*};\n        assert_eq!(\n            cake::Entity::delete_by_id(1)\n                .build(DbBackend::Sqlite)\n                .to_string(),\n            r#\"DELETE FROM \"cake\" WHERE \"cake\".\"id\" = 1\"#,\n        );\n    }\n\n    #[test]","sourceCodeStart":1045,"sourceCodeEnd":1081,"githubUrl":"https://github.com/SeaQL/sea-orm/blob/e29bcd1b417c41a553b386fe94511d7c64a1c8ec/sea-orm-sync/src/entity/base_entity.rs#L1045-L1081","documentation":"`delete_by_id` builds an `ActiveModel` for the primary key, then calls `Delete::one(am).validate()` and unwraps with `.expect(\"Must be valid\")`. The validation asserts that the delete statement has a WHERE clause on the primary key. Panicking here means the constructed delete was structurally invalid — in practice only reachable if the primary key columns could not be set (e.g. arity mismatch path fell through) or the model type is misconfigured.","triggerScenarios":"Calling `EntityTrait::delete_by_id(value)` where the supplied value cannot be converted into the primary key column set correctly, or a custom entity whose PrimaryKey definition is inconsistent so the ActiveModel is built without its key columns set.","commonSituations":"Deleting by a composite key while passing a single value, passing the wrong id type after a schema refactor, or hand-rolled entity definitions with broken primary key metadata.","solutions":["Pass the primary key value in the exact form the entity expects (single value for single-column key, tuple for composite keys).","Regenerate/fix the entity's PrimaryKey definition so it matches the database schema.","Prefer `Entity::delete_by_id(pk).exec(db)` usage patterns from docs and verify the entity compiles tests that call delete_by_id."],"exampleFix":"// before\nMyEntity::delete_by_id((id,)) // tuple for single-column key is wrong\n// after\nMyEntity::delete_by_id(id) // plain value for single-column primary key","handlingStrategy":"validation","validationCode":"// check arity of the key you are about to delete by\nlet expected: usize = <MyEntity::PrimaryKey as PrimaryKeyTrait>::ValueType::arity();\n// single-column key: pass a plain value; composite: pass a tuple of the same length","typeGuard":"fn valid_delete_key<E: EntityTrait>(pk: E::PrimaryKey) -> bool { true } // compile-time: only correctly-typed keys convert","tryCatchPattern":"// delete_by_id panics internally, so validate types first; runtime errors from exec(db) are catchable:\nmatch MyEntity::delete_by_id(pk).exec(db).await {\n    Ok(res) => println!(\"deleted {}\", res.rows_affected),\n    Err(DbErr::RecordNotFound(_)) => { /* nothing to delete */ }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Pass primary key values typed as the entity's PrimaryKey, not raw scalars.","Use tuples for composite keys with the exact same length.","Regenerate entities after schema changes."],"tags":["panic","delete","primary-key","validation"],"backgroundTag":"internal-invariant-violation","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"}