{"record":{"id":"03332e6ff4f6aa57","repo":"SeaQL/sea-orm","slug":"should-not-retrieve-last-insert-id-this-way","errorCode":null,"errorMessage":"Should not retrieve last_insert_id this way","messagePattern":"Should not retrieve last_insert_id this way","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sea-orm-sync/src/executor/execute.rs","lineNumber":51,"sourceCode":"}\n\n// ExecResult //\n\nimpl ExecResult {\n    /// The auto-increment primary key value assigned by the database on the\n    /// most recent `INSERT`.\n    ///\n    /// # Panics\n    ///\n    /// PostgreSQL does not expose `last_insert_id` directly — use\n    /// `exec_with_returning` / `exec_with_returning_keys` instead.\n    pub fn last_insert_id(&self) -> u64 {\n        match &self.result {\n            #[cfg(feature = \"sqlx-mysql\")]\n            ExecResultHolder::SqlxMySql(result) => result.last_insert_id(),\n            #[cfg(feature = \"sqlx-postgres\")]\n            ExecResultHolder::SqlxPostgres(_) => {\n                panic!(\"Should not retrieve last_insert_id this way\")\n            }\n            #[cfg(feature = \"sqlx-sqlite\")]\n            ExecResultHolder::SqlxSqlite(result) => {\n                let last_insert_rowid = result.last_insert_rowid();\n                if last_insert_rowid < 0 {\n                    unreachable!(\"negative last_insert_rowid\")\n                } else {\n                    last_insert_rowid as u64\n                }\n            }\n            #[cfg(feature = \"rusqlite\")]\n            ExecResultHolder::Rusqlite(result) => {\n                let last_insert_rowid = result.last_insert_rowid;\n                if last_insert_rowid < 0 {\n                    unreachable!(\"negative last_insert_rowid\")\n                } else {\n                    last_insert_rowid as u64\n                }","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/SeaQL/sea-orm/blob/e29bcd1b417c41a553b386fe94511d7c64a1c8ec/sea-orm-sync/src/executor/execute.rs#L33-L69","documentation":"ExecResult::last_insert_id() only yields a value for backends that report it natively (MySQL). On PostgreSQL the driver returns no last_insert_id — the inserted key is delivered through RETURNING via exec_with_returning / exec_with_returning_keys — so calling this getter on a Postgres result hits an intentional panic. The at-fault input is an ExecResult produced by a PostgreSQL insert.","triggerScenarios":"Thrown at sea-orm-sync/src/executor/execute.rs:51 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["On PostgreSQL use exec_with_returning or exec_with_returning_keys to obtain the inserted primary key.","Branch on DbBackend (or ExecResultHolder variant) before reading last_insert_id, and use the backend-appropriate API.","Avoid this getter in backend-agnostic insert code; use the higher-level insert()/save() APIs that already return the model with its new id."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}