Skip to content

updateWhere fails to deserialize returned rows when a field uses a column= override #5911

Closed

Description

@vchrisb

Describe the bug

When a table model has a field with a column= override, Model.db.updateWhere(...) throws after the UPDATE has run. The returned rows are keyed by database column name (auth_user_id), but the generated fromJson reads the field name (authUserId). That field comes back null and deserialization fails.

Cause: insert and update build their returning list with buildReturningClause() (sql_query_builder.dart), which renames mapped columns to their field names when table.hasColumnMapping is true. updateWhere (database_connection.dart:494, and :486–487 with limit/orderBy) and updateById (:403) use a plain RETURNING * instead. I reproduced updateWhere; updateById has the same code and should fail the same way.

Passing noReturn: true to updateWhere avoids it, because no rows are deserialized. updateById has no such option.

Model:

class: Account
serverOnly: true
table: account
fields:
  id: UuidValue, defaultModel=random_v7
  authUserId: UuidValue, column=auth_user_id
  language: String, default='de'

To reproduce

await Account.db.updateWhere(
  session,
  columnValues: (t) => [t.language('en')],
  where: (t) => t.id.equals(accountId),
);
type 'Null' is not a subtype of type 'String' in type cast
  package:serverpod_serialization/src/extensions/serialization_extensions.dart 61:43  UuidValueJsonExtension.fromJson
  package:<project>/src/generated/.../account.dart                                     new Account.fromJson
  package:<project>/src/generated/protocol.dart                                        Protocol.deserialize
  package:serverpod_database/src/adapters/postgres/database_connection.dart 503:72    PostgresDatabaseConnection.updateWhere

The UPDATE has already been applied when the exception is thrown. Outside a transaction, the row is changed but the call reports failure.

Expected behavior

updateWhere and updateById return the updated rows, using buildReturningClause(table) for their RETURNING list like insert and update do.

Library version

4.1.0-rc.1 (same code in 4.0.4)

Platform information

Server: Dart 3.13.7 (Windows x64), PostgreSQL (Serverpod embedded)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: endpointsIssues related to endpoints.bugSomething isn't working

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions