Skip to content

fix: Accept set literals in inSet and notInSet on id columns - #5948

Merged
FXschwartz merged 3 commits into
serverpod:mainfrom
BiAksoy:fix/id-column-inset-nullable-id
Nov 25, 2026
Merged

FXschwartz merged 3 commits into
serverpod:mainfrom
BiAksoy:fix/id-column-inset-nullable-id

Conversation

@BiAksoy

@BiAksoy BiAksoy commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

t.id.inSet({...}) and t.id.notInSet({...}) throw type '_Set<int?>' is not a subtype of type 'Set<int>' of 'values' (or the UuidValue equivalent) when the set literal is not given an explicit type. Generated tables extend Table<int?>, so t.id is statically a ColumnComparable<int?> and the literal is inferred as a Set<int?>, but the Table constructor builds a ColumnInt and casts it to that type:

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, and the run-time check of inSet(Set<int> values) then rejects the Set<int?>.

This PR builds the id column as ColumnIntId or ColumnUuidId, subclasses of ColumnInt and ColumnUuid whose inSet and notInSet take a Set<T?>. Widening the parameter is a valid override, and it makes the run-time parameter type match what callers of ColumnComparable<int?> already pass. They stay a ColumnInt and a ColumnUuid, so the adapters' is ColumnInt checks keep working:

if (table.id is ColumnInt) return build(['NULL'], []);

Since the static type admits null, the overrides handle it the way equals(null) and notEquals(null) do. That matters for the id of a table joined through an optional relation, which is null when the relation is not set: t.mayor.id.inSet({alex.id, null}) builds (... IN (...) OR ... IS NULL), and notInSet with a null builds (... NOT IN (...) AND ... IS NOT NULL). Sets without null produce the same SQL as before.

Other columns are not affected: a field such as int? companyId is generated as a ColumnInt, so its inSet already takes a Set<int> statically, and the analyzer rejects a Set<int?>. Generated tables declare every other column with the class it is built as; t.id is the only one declared with a different type argument. On it, inSet and notInSet are the only operations whose run-time parameter type is narrower than the static one: equals and notEquals take int? either way, and the comparison operators take dynamic.

The two classes are marked @internal and hidden from the package export; only Table creates them. castToIdType() is left as it is: it is public, and the adapters still need it, because the ids they read from a TableRow are dynamic and would otherwise form a Set<dynamic>.

Before, the new unit test for Table<int?>:

type '_Set<int?>' is not a subtype of type 'Set<int>' of 'values'

After:

"test"."id" IN (1, 2)

The fix and its unit tests are the first commit, the integration tests the second.

Tests:

  • packages/serverpod_database/test/columns/column_id_test.dart checks the SQL that inSet and notInSet build on the id column of Table<int?> (with and without null in the set, and with an empty set) and of Table<UuidValue?>. 10 of its 12 tests fail on main with the error above. The remaining two, on Table<UuidValue>, already passed and check that a non-nullable id is unchanged.
  • test_integration/column_operations/column_id_test.dart in serverpod_test_server and in serverpod_test_sqlite_server runs inSet and notInSet on an int id (TownInt), on a UuidValue id (CompanyUuid), and on the id of an optional relation with null in the set (TownInt.mayor). All six fail on main with the type error, on PostgreSQL and on SQLite.

With the change, the serverpod_database unit tests pass (dart test -x integration), as do test_integration/column_operations and test_integration/database_operations of serverpod_test_server on embedded PostgreSQL (-x redis). In the full serverpod_test_sqlite_server integration suite, two tests in test_tools/ephemeral_database_test.dart fail on macOS, with and without this change, because they compare a temp path under /var with the resolved /private/var path.

Fixes #5947.

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

Breaking changes

None. Calls that compiled before compile the same way, and sets without null produce the same SQL.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Generated tables extend `Table<int?>`, so `t.id` is typed
`ColumnComparable<int?>` and a set literal passed to `inSet` is a
`Set<int?>`. The column was a `ColumnInt` cast to that type, whose
`inSet(Set<int>)` rejected the set at run time.

Build the id column as a subclass of `ColumnInt` or `ColumnUuid` whose
set operations take `Set<T?>`, treating null like `equals(null)` does.

@FXschwartz FXschwartz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @BiAksoy, thanks for digging into this one! The fix looks good to me and I couldn't find any other column that has the same static/runtime mismatch.

Just one super minor nit from my side:

@BiAksoy
BiAksoy requested a review from FXschwartz November 24, 2026 19:16

@FXschwartz FXschwartz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks again @BiAksoy

@FXschwartz
FXschwartz merged commit e5bd533 into serverpod:main Nov 25, 2026
482 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

t.id.inSet({...}) and t.id.notInSet({...}) throw a type error at run time

2 participants