fix: PostgreSQL insert_row fails for tables with text primary keys#22
fix: PostgreSQL insert_row fails for tables with text primary keys#22mikkeldamsgaard merged 1 commit intomainfrom
Conversation
The insert_row function always used RETURNING COALESCE(CAST(id AS BIGINT), 0) even when no auto_id_column was specified, defaulting to the id column. For tables with text primary keys (like NetBird's accounts, users, groups), CAST to BIGINT fails with a generic db error. Fix: Only use RETURNING clause when auto_id_column is explicitly set. When no auto_id is needed, use a simple INSERT without RETURNING.
There was a problem hiding this comment.
Pull request overview
This PR fixes PostgreSQL seeding failures when inserting into tables that have non-numeric (e.g., text) primary keys by avoiding a RETURNING ... CAST(... AS BIGINT) clause unless an auto_id_column is explicitly configured.
Changes:
- Update
PostgresDb::insert_rowto only include aRETURNINGclause whenauto_id_columnisSome(...). - Use a plain
INSERT(noRETURNING) when no auto-generated numeric ID is required, returningOk(None).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if let Some(auto_col) = auto_id_column { | ||
| let returning_col = sanitize_identifier(auto_col); | ||
| let sql = format!( | ||
| "INSERT INTO \"{}\" ({}) VALUES ({}) RETURNING COALESCE(CAST(\"{}\" AS BIGINT), 0)", | ||
| sanitize_identifier(table), | ||
| col_list.join(", "), | ||
| value_list.join(", "), | ||
| returning_col | ||
| ); | ||
| let row = self | ||
| .client | ||
| .query_one(&sql, &[]) | ||
| .map_err(|e| format!("inserting row into '{}': {}", table, e))?; | ||
| let id: i64 = row.get(0); | ||
| Ok(Some(id)) | ||
| } else { | ||
| let sql = format!( | ||
| "INSERT INTO \"{}\" ({}) VALUES ({})", | ||
| sanitize_identifier(table), | ||
| col_list.join(", "), | ||
| value_list.join(", "), | ||
| ); | ||
| self.client | ||
| .execute(&sql, &[]) | ||
| .map_err(|e| format!("inserting row into '{}': {}", table, e))?; | ||
| Ok(None) | ||
| } |
There was a problem hiding this comment.
The new Postgres insert_row behavior (skipping RETURNING when auto_id_column is None) fixes the reported bug, but it isn’t covered by tests. There are existing Postgres seeding integration tests, but none exercise inserting into a table with a non-numeric primary key and no auto_id config; adding an integration test/fixture for that case would prevent regressions.
The insert_row function always used RETURNING COALESCE(CAST(id AS BIGINT), 0) even when no auto_id_column was specified, defaulting to the id column. For tables with text primary keys (like NetBird's accounts, users, groups), CAST to BIGINT fails with a generic db error. Fix: Only use RETURNING clause when auto_id_column is explicitly set. When no auto_id is needed, use a simple INSERT without RETURNING.
* feat: add integration tests with docker-compose (#7) - wait-for, render, fetch, exec, seed PostgreSQL/MySQL with cross-table refs, idempotency, reset, create database/schema, create-if-missing tests * fix: use inline escaped values in postgres insert_row/row_exists to fix TEXT-to-INTEGER coercion * fix: reverse seed set order during reset to respect foreign key constraints * fix: separate reset (reverse) and seed (forward) passes for FK-safe reset mode * refactor: remove placeholder seed_sets from test specs and move inline seed specs to files * Update .github/workflows/integration.yml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix: PostgreSQL insert_row fails for tables with text primary keys (#22) The insert_row function always used RETURNING COALESCE(CAST(id AS BIGINT), 0) even when no auto_id_column was specified, defaulting to the id column. For tables with text primary keys (like NetBird's accounts, users, groups), CAST to BIGINT fails with a generic db error. Fix: Only use RETURNING clause when auto_id_column is explicitly set. When no auto_id is needed, use a simple INSERT without RETURNING. * fix: address PR #21 review comments - remove unused CI client install, add cfg feature guards, fix README test name, document escaped literals rationale, reword CHANGELOG --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
The insert_row function always used
RETURNING COALESCE(CAST("id" AS BIGINT), 0)even when no auto_id_column was specified, defaulting to the "id" column. For tables with text primary keys (like NetBird's accounts, users, groups), CAST to BIGINT fails with a generic "db error".Fix: Only use
RETURNINGclause whenauto_id_columnis explicitly set. When no auto_id is needed, use a simple INSERT without RETURNING.