{"record":{"id":"3c417412e9b753cc","repo":"nautechsystems/nautilus_trader","slug":"failed-to-insert-into-general-table-e","errorCode":null,"errorMessage":"Failed to insert into general table: {e}","messagePattern":"Failed to insert into general table: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":76,"sourceCode":"            .execute(pool)\n            .await\n            .map(|_| ())\n            .map_err(|e| anyhow::anyhow!(\"Failed to truncate tables: {e}\"))\n    }\n\n    /// Inserts a raw key-value entry into the `general` table via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the INSERT operation fails.\n    pub async fn add(pool: &PgPool, key: String, value: Vec<u8>) -> anyhow::Result<()> {\n        sqlx::query(\"INSERT INTO general (id, value) VALUES ($1, $2)\")\n            .bind(key)\n            .bind(value)\n            .execute(pool)\n            .await\n            .map(|_| ())\n            .map_err(|e| anyhow::anyhow!(\"Failed to insert into general table: {e}\"))\n    }\n\n    /// Loads all entries from the `general` table via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SELECT operation fails.\n    pub async fn load(pool: &PgPool) -> anyhow::Result<AHashMap<String, Vec<u8>>> {\n        sqlx::query_as::<_, GeneralRow>(\"SELECT * FROM general\")\n            .fetch_all(pool)\n            .await\n            .map(|rows| {\n                let mut cache: AHashMap<String, Vec<u8>> = AHashMap::new();\n                for row in rows {\n                    cache.insert(row.id, row.value);\n                }\n                cache\n            })","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L58-L94","documentation":"`DatabaseQueries::add` inserts a raw key-value entry into the `general` table and wraps any sqlx failure in this anyhow error. Typical underlying causes are connection failure, a missing `general` table (migrations not applied), or a constraint violation such as a duplicate primary key when the same key is inserted twice.","triggerScenarios":"Calling `DatabaseQueries::add(pool, key, value)` when the connection/pool is broken, the `general` table doesn't exist, the `id` primary key is violated by a repeated key, or the `value` bytes don't fit the column type.","commonSituations":"Writing cache entries to a database where migrations never ran; re-adding an existing key on restart; Postgres restart or network blip dropping the connection mid-write.","solutions":["Run migrations so the `general` table exists.","Check the inner sqlx error: for duplicate-key errors, delete the row first or use ON CONFLICT upsert semantics.","Verify pool connectivity and that the DB user has INSERT privilege on `general`.","Confirm key/value encodings match the column types (e.g. text/bytea)."],"exampleFix":"// before\nDatabaseQueries::add(&pool, key, value).await?;\n// after\nsqlx::query(\"DELETE FROM general WHERE id = $1\").bind(&key).execute(&pool).await?;\nDatabaseQueries::add(&pool, key, value).await?;","handlingStrategy":"try-catch","validationCode":"let exists: bool = sqlx::query_scalar(\n    \"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'general')\",\n).fetch_one(pool).await?;\nif !exists {\n    return Err(anyhow::anyhow!(\"'general' table missing — run migrations\"));\n}","typeGuard":null,"tryCatchPattern":"match DatabaseQueries::add(&pool, key, value).await {\n    Err(e) if e.to_string().contains(\"duplicate key\") => {\n        tracing::warn!(\"key {key} already present, skipping\");\n    }\n    result => result?,\n}","preventionTips":["Run migrations before writing to the cache database.","Handle duplicate keys deliberately (delete-then-insert or ON CONFLICT) rather than relying on a bare INSERT.","Keep the connection pool healthy and verify connectivity on startup.","Confirm the DB user has INSERT privilege on `general`."],"tags":["postgres","sqlx","database","insert"],"backgroundTag":"database-write-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}