From d4540a1de7af63a4ef5ff47530b009d45b95d066 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Fri, 11 Mar 2022 12:34:19 -0600 Subject: [PATCH] Make column ordering explicit in `createRecord` return. The Nim [Row][nim-row] implementation only supports positional identification of columns. In other words, there is nothing to tell us which column is in which position. Because of this, we always create SQL statements which explicitly name the columns we wish to receive so that we know the order of columns and can rebuild models appropriately. `createRule` wasn't doing this but naively using `RETURNING *`. This still works as long as the field ordering in the Nim model class match the default column ordering returned by the database, but confuses columns otherwise. This fixes that by specifying explicitly the column ordering as we do in other places. [nim-row]: https://nim-lang.org/docs/db_postgres.html#Row --- fiber_orm.nimble | 2 +- src/fiber_orm.nim | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/fiber_orm.nimble b/fiber_orm.nimble index 2e5fe44..5ca3522 100644 --- a/fiber_orm.nimble +++ b/fiber_orm.nimble @@ -1,6 +1,6 @@ # Package -version = "1.0.0" +version = "1.0.1" author = "Jonathan Bernard" description = "Lightweight Postgres ORM for Nim." license = "GPL-3.0" diff --git a/src/fiber_orm.nim b/src/fiber_orm.nim index b3be8b0..a5118a0 100644 --- a/src/fiber_orm.nim +++ b/src/fiber_orm.nim @@ -34,13 +34,11 @@ proc createRecord*[T](db: DbConn, rec: T): T = var mc = newMutateClauses() populateMutateClauses(rec, true, mc) - # Confusingly, getRow allows inserts and updates. We use it to get back the ID - # we want from the row. let sqlStmt = "INSERT INTO " & tableName(rec) & " (" & mc.columns.join(",") & ") " & " VALUES (" & mc.placeholders.join(",") & ") " & - " RETURNING *" + " RETURNING " & columnNamesForModel(rec).join(",") log().debug "createRecord: [" & sqlStmt & "]" let newRow = db.getRow(sql(sqlStmt), mc.values)