Repository navigation
Make key_source restrict-only: add key_source_restriction, deprecate parent-redefining overrides #1523
Description
Summary
Make key_source a restrict-only surface. Today key_source can be overridden freely, including in ways that redefine the parent join — and that is the source of subtle, hard-to-diagnose bugs. Introduce a first-class key_source_restriction that applies a restriction to the default key_source without changing the parents, and steer users (and the docs) toward it.
Background
key_source defaults to the join of the tables referenced by the foreign keys in a computed/imported table's primary key (Autopopulate → Key Source Calculation). populate() iterates over it, passing each key to make(key).
The default is almost always correct. In practice, the only legitimate reason to modify it is to restrict it — compute a subset of the default source (e.g. a completion barrier that fires only when all members of a batch have results). Restricting keeps the parents and the grain intact; each make(key) still receives a fully-formed primary key.
Overriding key_source to redefine the parent join — dropping a parent, adding one, or reconstructing the key from an aggregate — is a different and dangerous operation: it can change or drop primary-key attributes, so make(key) receives a key that is missing attributes or has attributes the table's primary key doesn't declare. The downstream effects (partial inserts, mismatched keys, silent miscomputation) are hard to reason about, and the framework was not designed for it.
Proposal
- Introduce
key_source_restriction— a declared restriction applied to the defaultkey_source. It may be any valid DataJoint restriction, including a restriction by another table (a dependency visible inkey_sourcebut not necessarily as a formal FK edge). The computed source isdefault_key_source & key_source_restriction; the parents and grain are never changed. - Deprecate user-facing redefinition of
key_source. Treatkey_sourceas an internal of the auto-populate machinery. Emit a warning when a subclass overrideskey_sourcein a way that changes the parent join. - Batching is a master–part concern, not a
key_sourceconcern. A run/cohort-level batch should be a master keyed on the batch with per-member results on a part — consistent with the DataJoint 2 rule that an auto-populated table introduces no new dimension (only its parts may). This removes the historical "batch by overridingkey_source" motivation.
Rollout (per the 2026-07-24 design discussion)
- 2.4: add
key_source_restriction; begin warning on parent-redefiningkey_sourceoverrides. - Docs now: remove
key_source-override guidance/recommendations from the documentation; documentkey_source_restrictionas the supported way to narrow a source. - Later: make
key_sourcerestriction-only (the override path removed as a user surface).
Docs task (coordinated)
datajoint-docs: pull any recommendation to override key_source, and document key_source_restriction + the "batch via master–part" guidance. (Autopopulate spec §2 and the how-to pages.)
Activity
dimitri-yatsenko commentedon Aug 17, 2026
A second, independent justification for restrict-only: it is exactly what makes refreshing the jobs table without importing pipeline code possible.
Today
refresh()requires the pipeline codebase because an overriddenkey_sourceis arbitrary Python — unserializable in general. Any orchestration around DataJoint (a cron, a scheduler, a dashboard) must therefore run refresh inside an environment with the pipeline installed, just to materialize pending jobs.After this change,
key_source = default FK-join & key_source_restriction, and the two halves have very different portability:That suggests a small addition to this proposal: at declaration (or worker registration), write the rendered
key_source_restrictionSQL into the jobs table's metadata, with a stamp for staleness. Every jobs table becomes self-describing for refresh, andbecomes a generic operation any actor with DB credentials can run — a plain cron, an external scheduler, or eventually a trigger/CDC event restricted to the arriving key. Restrictions whose values must be recomputed at refresh time (moving clock windows, external-state probes) remain the one case needing code, and under restrict-only they are visibly deviant rather than structurally indistinguishable from normal overrides.
Staleness also narrows: re-registration is needed only when the declared restriction changes, not on every code deployment.
Worth weighing in the 2.4 design: whether the stored/rendered restriction (and a
refresh()mode that executes from it) lands together withkey_source_restrictionor as a follow-up.github-actions commentedon Oct 2, 2026
This issue is stale because it has been open for 45 days with no activity.