Repository navigation
fix: Return field names from updateWhere and updateById for explicit columns - #5913
Conversation
…columns The Postgres updateWhere and updateById returned rows with RETURNING *, so columns with a column= override came back under their database name and failed to deserialize. Build the returning clause with buildReturningClause, as insert and update already do, and qualify the CTE ORDER BY so it still resolves to the database columns. Fixes serverpod#5911
marcelomendoncasoares
left a comment
There was a problem hiding this comment.
Thanks for this fix, @vchrisb! Only a small request to increase the coverage.
|
The new test model exposed a bug in rename detection The Why it happens: Before this PR, no model had explicit column names that collided with another field's name, so the case never came up. Potential Fix: if the source column has no stored Related:
@marcelomendoncasoares what do you suggest how to proceed? |
@vchrisb Yeah, we should not try to fix the rename issue it here. Inclusively, the fix might not be as straightforward, because the current fallback exists to allow renames that transfer a column to another field to be done in two steps. If there is no other way to create a test "where a database column name is also the returned alias of a different field", we can skip this for now and merge. |
10a17de to
7aad16c
Compare
|
I removed the failing test and will create a separate issue for that. |
updateWhereandupdateByIdfail to deserialize their returned rows on Postgres when a model field has acolumn=override.Both methods used
RETURNING *, so the rows came back with database column names (e.g.auth_user_id), but the generatedfromJsonexpects field names (authUserId). TheUPDATEitself succeeded, but the call then threw. Outside a transaction this left the row modified while the caller saw a failure.insertandupdatealready avoid this by usingbuildReturningClause.This PR uses
buildReturningClausein all three places that returned rows:updateById: the returned columns are renamed to field names.updateWherewithout limit, offset or ordering: same change.updateWherewith limit, offset or ordering: the outerSELECTfrom theupdatedCTE now renames the columns. TheORDER BYnow qualifies columns with the CTE name ("updated"."col"), so it still sorts by the database columns rather than the renamed output columns.The returning clause uses the bare table name as its qualifier because these
UPDATEstatements don't alias the table. That keeps it valid for schema-qualified tables too.Tables without column overrides still get
RETURNING */SELECT *. The only change to their SQL is the qualifiedORDER BY. SQLite was not affected, because it already renames returned rows itself.Integration tests in
explicit_column_name/crud_test.dartcoverupdateById,updateWhere,updateWherewithorderByandlimit, andupdateWherewithlimitonly. All of them fail without the fix and pass with it. The existingupdate_where,update_by_idandsql_injectionintegration suites also pass.Fixes #5911
Pre-launch Checklist
///), and made sure t the same style as other Serverpod documentation. I checked spelling and grammar.If you need help, consider asking for advice on the discussion board.
Breaking changes
None.