{"record":{"id":"91be8de0a5809579","repo":"SeaQL/sea-orm","slug":"must-be-valid-91be8d","errorCode":null,"errorMessage":"Must be valid","messagePattern":"Must be valid","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/entity/base_entity.rs","lineNumber":1085,"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":1067,"sourceCodeEnd":1103,"githubUrl":"https://github.com/SeaQL/sea-orm/blob/e29bcd1b417c41a553b386fe94511d7c64a1c8ec/src/entity/base_entity.rs#L1067-L1103","documentation":"`delete_by_id` builds a Delete ActiveModel from the given id value and validates it with `.validate().expect(\"Must be valid\")` (src/entity/base_entity.rs:1085). Validation should always pass for a properly constructed key-backed delete, so this panic indicates the constructed ActiveModel failed validation — e.g. a key value whose type doesn't match the primary key column type.","triggerScenarios":"Calling `Entity::delete_by_id()` with a value that sets a primary key column violating validation rules (wrong value type pushed into the ActiveModel, or composite key helper mismatch) — validation then fails and the expect panics.","commonSituations":"Passing a wrongly-typed id (string vs integer) that coerces into the ActiveModel but fails column validation; composite keys where the value shape does not match column order; sea-orm-cli-generated i64 keys vs code supplying i32 with mismatched DeriveValueType wrappers.","solutions":["Call delete_by_id with a value matching the primary key's declared type exactly (e.g. i64 for SQLite-generated entities).","For composite keys, pass the full tuple in the declared column order.","Convert through `delete_by_id(key.into_value())`-style helpers instead of hand-building values.","If validation rejects a correct key, replace with `Entity::delete_by_id` alternative: build the ActiveModel yourself and call `Delete::one(am).exec(db)` to get a DbErr instead of a panic."],"exampleFix":"// before\nentity::Entity::delete_by_id(\"42\".to_string()).exec(db).await?;\n// after (primary key is i64)\nentity::Entity::delete_by_id(42i64).exec(db).await?;","handlingStrategy":"validation","validationCode":"// confirm the id type matches the primary key before delete_by_id\nlet pk_type = <entity::Entity as EntityTrait>::PrimaryKey::iter().next().unwrap().value_type();\nassert!(matches!(id, v if std::mem::discriminant(&v) == std::mem::discriminant(&pk_type)));","typeGuard":"fn id_matches_pk(id: &Value) -> bool {\n    use sea_orm::Value;\n    matches!(id, Value::BigInt(_) | Value::TinyInt(_) | Value::SmallInt(_) | Value::Int(_))\n}","tryCatchPattern":"// avoid the expect by building the delete yourself to get a DbErr\nlet mut am = entity::ActiveModel { id: sea_orm::ActiveValue::Set(42), ..Default::default() };\nentity::Entity::delete(am).exec(db).await?;","preventionTips":["Always pass ids of the exact generated primary key type (i64 for SQLite by default)","Use tuple form for composite keys in declared column order","Prefer building the Delete statement manually if you need recoverable errors"],"tags":["delete","validation","panic","primary-key"],"backgroundTag":"invalid-argument-value","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"}