{"record":{"id":"9088214f8a6479a1","repo":"nautechsystems/nautilus_trader","slug":"failed-to-insert-into-trade-table-e","errorCode":null,"errorMessage":"Failed to insert into trade table: {e}","messagePattern":"Failed to insert into trade table: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":1263,"sourceCode":"                $1, $2, $3, $4::aggressor_side, $5, $6, $7, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP\n            )\n            ON CONFLICT (id)\n            DO UPDATE\n            SET\n                instrument_id = $1, price = $2, quantity = $3, aggressor_side = $4, venue_trade_id = $5,\n                ts_event = $6, ts_init = $7, updated_at = CURRENT_TIMESTAMP\n        \"#)\n            .bind(trade.instrument_id.to_string())\n            .bind(trade.price.to_string())\n            .bind(trade.size.to_string())\n            .bind(AggressorSidePg(trade.aggressor_side))\n            .bind(trade.trade_id.to_string())\n            .bind(trade.ts_event.to_string())\n            .bind(trade.ts_init.to_string())\n            .execute(pool)\n            .await\n            .map(|_| ())\n            .map_err(|e| anyhow::anyhow!(\"Failed to insert into trade table: {e}\"))\n    }\n\n    /// Loads all `TradeTick` entries for `instrument_id` via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SQL SELECT or deserialization fails.\n    pub async fn load_trades(\n        pool: &PgPool,\n        instrument_id: &InstrumentId,\n    ) -> anyhow::Result<Vec<TradeTick>> {\n        sqlx::query_as::<_, TradeTickRow>(\n            r#\"SELECT * FROM \"trade\" WHERE instrument_id = $1 ORDER BY ts_event ASC\"#,\n        )\n        .bind(instrument_id.to_string())\n        .fetch_all(pool)\n        .await\n        .map(|rows| rows.into_iter().map(|row| row.0).collect())","sourceCodeStart":1245,"sourceCodeEnd":1281,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L1245-L1281","documentation":"This error wraps any sqlx failure from the INSERT INTO `trade` statement in `add_trade`. The library converts the raw database error into a message naming the operation. It means the TradeTick row was not persisted.","triggerScenarios":"Calling `add_trade(pool, &trade)` when the `trade` table does not exist, a column type/constraint fails (e.g. instrument_id or enum casting), the connection drops mid-insert, or the pool is closed.","commonSituations":"Migrations not run in a new environment; schema drift after upgrading (missing columns); enum values not representable in the Postgres enum types; DB credentials or network failures.","solutions":["Run migrations so the `trade` table and its enum types exist","Check the wrapped sqlx error for constraint/type issues (column mismatch, invalid enum label) and align the schema or data","Verify pool connectivity before batch inserts","Use a transaction so failed trade inserts can be retried as a batch"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"sqlx::query(\"SELECT 1 FROM trade LIMIT 1\").fetch_optional(pool).await.map_err(|e| anyhow::anyhow!(\"trade table check: {e}\"))?;","typeGuard":null,"tryCatchPattern":"if let Err(e) = add_trade(&pool, &trade).await {\n    tracing::error!(\"trade insert failed: {e:#}\");\n    return Err(e.into());\n}","preventionTips":["Ensure migrations create the trade table and enum types before writes","Keep schema in sync with the library version","Batch inserts inside a transaction for atomicity"],"tags":["database","insert","sql","postgres"],"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-14T05:17:10.506Z"}