Skip to content

fix(declare): reject constant defaults on primary-key attributes - #1759

Open
godara-alt wants to merge 3 commits into
datajoint:masterfrom
godara-alt:fix/1712-reject-constant-pk-defaults
Open

godara-alt wants to merge 3 commits into
datajoint:masterfrom
godara-alt:fix/1712-reject-constant-pk-defaults

Conversation

@godara-alt

Copy link
Copy Markdown
Contributor

Summary

Fixes #1712.

Reject constant defaults on user-defined primary-key attributes at declaration time, while preserving generated CURRENT_TIMESTAMP defaults and existing behavior for secondary attributes.

Root Cause

The declaration compiler previously rejected nullable primary-key attributes but allowed constant defaults.

This permitted insertions without an explicitly supplied primary-key value, causing repeated insertions to reuse the same constant key.

Changes

  • Reject constant numeric and string defaults on primary-key attributes with a descriptive DataJointError.

  • Continue allowing generated CURRENT_TIMESTAMP defaults.

  • Preserve the framework-generated _singleton sentinel, restricting its exception to the internal declaration path.

  • Remove the redundant omega=0 default from the university test schema, where the CSV already supplies the key explicitly.

  • Add regression tests covering MySQL and PostgreSQL adapters, constant defaults, generated defaults, secondary attributes and singleton behavior.

Testing and Verification

Final commit: b43425beb2c6ef0c94627708c1602bb96eec8fb5

Verified using GitHub Actions on the fork:

Test | Result -- | -- Lint and formatting | Passed Linux unit tests | Passed Windows unit tests (Python 3.10, 3.14, 3.15) | Passed MySQL integration tests (6 configurations) | Passed Minimum dependency versions (Python 3.10) | Passed

Final result: All 12 checks passed, with zero failures and zero pending checks.

Scope

The changes are limited to:

  • src/datajoint/declare.py

  • tests/schema_university.py

  • tests/unit/test_primary_key_defaults.py

No new dependencies were introduced and no tests were disabled.

The branch is up to date with upstream master.

Ready for review.

…ajoint#1712)

Permit generated CURRENT_TIMESTAMP defaults, preserve secondary defaults,
and test declarations across MySQL and PostgreSQL adapters.
… test fixture

Allow the platform-created singleton sentinel its fixed default without exposing that exception to user declarations. Remove a redundant constant key default from the university fixture, whose CSV supplies an explicit key. Add regression tests.

@dimitri-yatsenko dimitri-yatsenko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you, @godara-alt. The rule matches #1712: a constant default on a key attribute now fails at declaration and names the attribute; CURRENT_TIMESTAMP keys, secondary defaults, nullable-key errors and the internal singleton key are unchanged. Eight of the new tests fail without the change, and the full suite passes. The docs how-to already states the rule.

Two regressions on tables that already exist, both reproduced on MySQL and PostgreSQL:

  1. alter() fails on any existing table whose key has a default in the database, even after the default is removed from the class. alter() re-parses the definition describe() reads from the server, which includes probe_id=1 : int32, and compile_attribute now raises. The message says to remove the default "from its declaration", which the user can't do: it lives in the database. Master alters the same table without error.
  2. The automatic lineage refresh fails without raising. On @schema decoration of an existing table with missing lineage rows, Table._refresh_lineage re-runs declare() on the class definition. If that definition still has omega=0-style key defaults, it now logs "Could not refresh lineage ..." and skips the repair, so later joins on that table can hit lineage errors.

Requested changes:

  • Apply the check only to new declarations the user writes: not to the definition alter() reconstructs from the server, and not when re-declaring a table that already exists (lineage refresh). A keyword flag set by those callers would do it.
  • Add integration tests on both backends: declare a table, set a key default with ALTER TABLE, then alter() a secondary attribute; and a CURRENT_TIMESTAMP(3) key that declares and generates distinct keys. The current tests only compile SQL.
  • Make the error message point to the alternatives: supply the key on insert, use CURRENT_TIMESTAMP for a generated key, or declare a table with an empty primary key for a one-row table (the omega=0 idiom the university fixture used).
  • Minor: the singleton exemption can check platform_singleton alone, since user attribute names can't start with _.

Release: definitions that declared before now fail, so this goes in 2.4 with #1712, not 2.3.7; I've set the milestone. It will need a small rebase after #1639, which changes the same declare.py signatures.

@dimitri-yatsenko dimitri-yatsenko added this to the v2.4 milestone Nov 30, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants