Nullable :target (optional FK) fields
The compiler forces :not-null t on any field with :target,
making it impossible to express an optional foreign-key reference
(e.g. "completed by" that starts empty). The generated DDL always
emits not null, and the validation layer (value-type-p) rejects
:NULL because the field's type test (e.g. #'stringp for :text)
doesn't accept it.
This surfaced in the chores model (Rose's model test): a :user
field with :target :users but no :not-null was compiled as
NOT NULL, causing insert to fail with "Invalid value type for
field :CHORES :USER. Expected type :TEXT, but got value :NULL"
when no user was selected.
Workaround today: use an M2M list field (= :type :list= +
:join-table) instead — the join table can be empty, so the
relationship is naturally optional.
See docs/model-reference.md → "Foreign keys (:target)" for the
new documentation of this behavior.
Decision needed: should the compiler respect an explicit
:not-null nil on :target fields (allowing nullable FKs), or
should this remain a constraint with M2M as the only option for
optional references?
yes, it should respect :not-null nil (or more precisely, the absence of :not-null t). The compiler shouldn't override an author's intent. If someone explicitly omits :not-null, the field should be nullable — that's how every other field type works. Forcing NOT NULL on FKs silently is a footgun, especially for AI-authored models.
This touches two layers:
- Compiler (
lisp/model.lisp) — stop forcing:not-null ton:targetfields; let the explicit value (or lack thereof) pass through to DDL generation - Validation (
lisp/predicates.lispor wherevervalue-type-plives) — acceptNULLfor nullable target fields
Want me to dig into the relevant source to map out the exact changes needed? And do you agree with the approach — respect the author's :not-null setting rather than forcing it?
Commit: 6f3f236 Nullable target
- Slot
- 23800
- Status
- 60 Done
- Tags
- compiler, bug
- Contributors
- Donnie