swiftdata-predicate-optional-coalesce-contains-trap
SwiftData #Predicate like `ids.contains($0.fromBinID ?? sentinelUUID)` compiles fine but throws NSInvalidArgumentException "unimplemented SQL generation … (bad LHS)" at fetch time against a real (even in-memory) ModelContext. Use when writing a #Predicate that filters an optional model column (nullable foreign-key / relationship ID) against a caller-supplied Set or Array of candidate IDs.
- License 许可
- MIT
- 最近更新
- 2026-11-07
- Stars
- ★ 1
- Forks
- 1
01 前言 Front Matter
- name
- swiftdata-predicate-optional-coalesce-contains-trap
- description
- SwiftData #Predicate like `ids.contains($0.fromBinID ?? sentinelUUID)` compiles fine but throws NSInvalidArgumentException "unimplemented SQL generation … (bad LHS)" at fetch time against a real (even in-memory) ModelContext. Use when writing a #Predicate that filters an optional model column (nullable foreign-key / relationship ID) against a caller-supplied Set or Array of candidate IDs.
03 SKILL.md 正文
SwiftData #Predicate: coalescing an optional column on the LHS of contains() throws at fetch time
Symptom
A #Predicate such as ids.contains($0.fromBinID ?? sentinelUUID) — matching
an optional model column against a candidate-ID list, the idiom developers
reach for constantly for nullable foreign keys — compiles cleanly and passes
any pure-logic unit test built around the predicate closure itself. It only
fails when actually run against a real ModelContext (a live store or an
in-memory one), throwing at fetch time:
NSInvalidArgumentException: unimplemented SQL generation … (bad LHS)
This happens identically whether the RHS candidate list is a Set or an
Array.
Root cause
SwiftData's predicate-to-SQL translator cannot generate SQL when the
left-hand expression of contains() is a computed value — specifically an
optional model column coalesced with ??. $0.fromBinID ?? sentinelUUID
turns the LHS into an expression the translator has no SQL form for, so it
throws rather than falling back to something correct. The predicate is legal
Swift and legal #Predicate syntax; the failure is purely in SwiftData's SQL
generation for this LHS shape, so nothing catches it before a real fetch runs.
Fix
Don't coalesce the optional column. Instead widen the candidate list itself
to the optional type — [UUID?] instead of [UUID] — so the predicate
becomes a bare column IN (…) comparison against an unmodified, optional
LHS:
// Throws NSInvalidArgumentException at fetch time (bad LHS):
let predicate = #Predicate<Item> { ids.contains($0.fromBinID ?? sentinelUUID) }
// Works: widen the RHS list to [UUID?] instead, leave the column bare.
let optionalIDs: [UUID?] = ids.map { $0 }
let predicate = #Predicate<Item> { optionalIDs.contains($0.fromBinID) }
nil simply never matches, because nil is never a member of optionalIDs
— the same filtering semantics as the coalesced version, achieved without a
computed LHS.
Evidence
Mined from Cubby iOS session logs (0015-2026-07-06-v0.2.2-fix-wave); adversarially verified. Checkpoint '2026-07-07 20:20':
"SwiftData #Predicate trap (worth remembering): coalescing an optional column on the LHS of contains() (
ids.contains($0.fromBinID ?? sentinel)) throws NSInvalidArgumentException: unimplemented SQL generation … (bad LHS) at FETCH time — with either a Set or Array. Fix: make the list [UUID?] so the column sits bare (column IN (…)); nil never matches because it's never in the list."
The failure was caught by a TDD RED against a real in-memory ModelContext;
the log explicitly notes "a pure-helper test would have missed it" — the
predicate closure alone, exercised without a ModelContext fetch, never
touches SwiftData's SQL generator.
Related skills
swiftdata-inmemory-test-harness— the in-memoryModelContextharness that caught this trap; a pure-logic unit test around the predicate closure would not have.swiftdata-cloudkit-model-rules— other SwiftData failures that compile clean and only surface at runtime against a real container/context.
04 同仓库更多 Skills
second-wind
Orchestrate long unattended Claude Code runs across multiple repos with the wind CLI. Use when the user wants to run Claude Code overnight, resume sessions after the 5-hour usage limit, run Claude in many repos at once via tmux, or says "set up second wind", "wind up", "overnight run", "resume after limit", "usage limit orchestrator".
structured-output-adaptive-thinking-token-bloat
A Claude API call using output_config.format (json_schema structured outputs) with thinking:{type:"adaptive"} burns most of its output budget on thinking (observed ~21k of ~24k tokens) without improving JSON quality, making the run up to ~5x more expensive than the same call with thinking omitted — use when calling the Messages API with a json_schema output format for extraction, ranking, classification, or summarization tasks, or when a schema-constrained call's usage.output_tokens is far larger than the returned JSON.
review
Apply a code-review lens to a diff, branch, or file — find correctness bugs, security issues, and quality problems, ranked by severity, with a concrete fix for each. Use when the user says "review this", "review my diff", "check this PR", "look this over before I commit", or "is this safe". Skip when the user wants the code written, not reviewed.
refresh
Rebuild the prompt-craft command registry and usage profile, and print a summary of installed commands and your personalized recommendations. Use when you install or update plugins and want the advisor to pick them up immediately, or say "refresh prompt-craft", "rebuild the command registry".
refactor
Apply a refactoring lens — restructure code without changing behavior, guarded by tests that pass before and after. Use when the user says "refactor", "clean this up", "extract this", "simplify this", "reduce duplication", or "make this readable" on existing working code. Skip when the change is meant to alter behavior (that's a feature or a fix).
plan
Decompose a non-trivial task into an explicit, tracked plan with goals and per-step acceptance criteria before writing code. Use when the user says "plan this", "break this down", "what's the plan", or asks for a multi-step feature/refactor that touches more than one file. Pairs with plan mode. Skip for single-edit changes that need no decomposition.