Repository navigation
Description
Describe the bug
Filtering a table by its id with a set literal passes the analyzer and throws when the query is built:
type '_Set<UuidValue?>' is not a subtype of type 'Set<UuidValue>' of 'values'
With the default int id the message is type '_Set<int?>' is not a subtype of type 'Set<int>' of 'values', so every table with the default id is affected. Tables whose id is a non-nullable UuidValue (Table<UuidValue>) are not. notInSet fails the same way. In an endpoint it shows up as a 500 on the first call.
Generated tables extend Table<int?> (or Table<UuidValue?>), so t.id is statically a ColumnComparable<int?>, its inSet takes a Set<int?>, and a literal like {1, 2} or {for (final e in employees) e.companyId} is inferred as one. The Table constructor builds a ColumnInt, which is a ColumnComparable<int>, and casts it to ColumnComparable<T_ID>:
serverpod/packages/serverpod_database/lib/src/concepts/table.dart
Lines 100 to 118 in 0bd8e09
| if (equalsType<T_ID, int>()) { | |
| id = | |
| ColumnInt( | |
| 'id', | |
| this, | |
| hasDefault: true, | |
| ) | |
| as ColumnComparable<T_ID>; | |
| } else if (equalsType<T_ID, UuidValue>()) { | |
| id = | |
| ColumnUuid( | |
| 'id', | |
| this, | |
| hasDefault: true, | |
| ) | |
| as ColumnComparable<T_ID>; | |
| } else { | |
| throw Exception('Unsupported id type: $T_ID'); | |
| } |
The cast succeeds because Dart generics are covariant, but the column's inSet and notInSet check their argument against Set<int> at run time:
serverpod/packages/serverpod_database/lib/src/concepts/columns.dart
Lines 681 to 708 in 0bd8e09
| Expression inSet(Set<T> values) { | |
| if (values.isEmpty) { | |
| return Constant.bool(false); | |
| } | |
| var valuesAsExpressions = values | |
| .map((e) => _encodeValueForQuery(e)) | |
| .toList(); | |
| return _InSetExpression(this, valuesAsExpressions); | |
| } | |
| /// Creates and [Expression] checking if the value in the column is NOT | |
| /// included in the specified set of values. | |
| /// If the set is empty, the expression will always be true and match all | |
| /// rows. | |
| Expression notInSet(Set<T> values) { | |
| if (values.isEmpty) { | |
| return Constant.bool(true); | |
| } | |
| var valuesAsExpressions = values | |
| .map((e) => _encodeValueForQuery(e)) | |
| .toList(); | |
| return _NotInSetExpression(this, valuesAsExpressions) | | |
| _IsNullExpression(this); | |
| } |
The adapters avoid this for their own table.id.inSet calls by converting the ids with castToIdType() first:
serverpod/packages/serverpod_database/lib/src/adapters/postgres/database_connection.dart
Line 541 in 0bd8e09
| table.id.inSet(rows.map((row) => row.id!).castToIdType().toSet()), |
serverpod/packages/serverpod_database/lib/src/concepts/columns.dart
Lines 1140 to 1151 in 0bd8e09
| /// An extension on iterable for Id columns. | |
| extension IdColumnIterable on Iterable { | |
| /// Casts the elements of the iterable to the correct id type. | |
| Iterable castToIdType() { | |
| if (first is int) { | |
| return cast<int>(); | |
| } else if (first is UuidValue) { | |
| return cast<UuidValue>(); | |
| } | |
| throw Exception('Unsupported id column type: ${first.runtimeType}'); | |
| } |
To reproduce
No database is needed, the error is thrown while the expression is built. With serverpod_database: 4.1.0 and uuid as dependencies:
import 'package:serverpod_database/serverpod_database.dart';
import 'package:uuid/uuid.dart';
void attempt(String label, void Function() f) {
try {
f();
print('$label: ok');
} catch (e) {
print('$label: ${e.runtimeType}: $e');
}
}
void main() {
final uuidTable = Table<UuidValue?>(tableName: 'site');
final intTable = Table<int?>(tableName: 'gauge');
final ids = [const Uuid().v7obj(), const Uuid().v7obj()];
print('uuid id column runtime type: ${uuidTable.id.runtimeType}');
attempt('uuid inSet({...})', () => uuidTable.id.inSet({for (final i in ids) i}));
attempt('uuid inSet(<UuidValue>{...})', () => uuidTable.id.inSet(<UuidValue>{for (final i in ids) i}));
attempt('uuid equals(id)', () => uuidTable.id.equals(ids.first));
attempt('uuid notInSet({...})', () => uuidTable.id.notInSet({ids.first}));
attempt('int inSet({1, 2})', () => intTable.id.inSet({1, 2}));
attempt('int inSet(<int>{1, 2})', () => intTable.id.inSet(<int>{1, 2}));
}uuid id column runtime type: ColumnUuid
uuid inSet({...}): _TypeError: type '_Set<UuidValue?>' is not a subtype of type 'Set<UuidValue>' of 'values'
uuid inSet(<UuidValue>{...}): ok
uuid equals(id): ok
uuid notInSet({...}): _TypeError: type '_Set<UuidValue?>' is not a subtype of type 'Set<UuidValue>' of 'values'
int inSet({1, 2}): _TypeError: type '_Set<int?>' is not a subtype of type 'Set<int>' of 'values'
int inSet(<int>{1, 2}): ok
Table<UuidValue?> and Table<int?> are what generated tables extend. In a server project I first hit it with a model declared id: UuidValue?, defaultPersist=random_v7 and a find whose where was (t) => t.id.inSet({...}) over ids collected from other rows. Giving the literal an explicit type, <UuidValue>{...}, works around it.
Expected behavior
t.id.inSet({1, 2}) builds "gauge"."id" IN (1, 2), as it does on any other int column, and notInSet likewise. If a set the static type allows cannot be used, the analyzer should reject it rather than the call failing at run time.
Library version
serverpod 4.0.4 in a project (UUID ids), serverpod_database 4.1.0 (script above), and main at 0bd8e09, where the code linked above is unchanged. Dart 3.13.7.
Platform information
macOS 27.0.1 (arm64).
Additional context
- I have a fix with tests and can open a PR. It gives the id column its own subclass of
ColumnIntandColumnUuidwhoseinSetandnotInSetaccept a set of nullable values, so the static type and the run-time type agree. A null in the set is handled the wayequals(null)is, since the id of a table joined through an optional relation can be null. - I did not find an existing issue for this.
How experienced are you with this library?
None
Are you interested in working on a PR for this?
- I want to work on this
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity