Repository navigation
fix(declare): reject constant defaults on primary-key attributes - #1759
Open
godara-alt wants to merge 3 commits into
Open
godara-alt wants to merge 3 commits into
godara-alt wants to merge 3 commits into
Conversation
…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
requested changes
Nov 30, 2026
dimitri-yatsenko
left a comment
Member
There was a problem hiding this comment.
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:
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 definitiondescribe()reads from the server, which includesprobe_id=1 : int32, andcompile_attributenow 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.- The automatic lineage refresh fails without raising. On
@schemadecoration of an existing table with missing lineage rows,Table._refresh_lineagere-runsdeclare()on the class definition. If that definition still hasomega=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, thenalter()a secondary attribute; and aCURRENT_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_TIMESTAMPfor a generated key, or declare a table with an empty primary key for a one-row table (theomega=0idiom the university fixture used). - Minor: the singleton exemption can check
platform_singletonalone, 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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1712.
Reject constant defaults on user-defined primary-key attributes at declaration time, while preserving generated
CURRENT_TIMESTAMPdefaults 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_TIMESTAMPdefaults.Preserve the framework-generated
_singletonsentinel, restricting its exception to the internal declaration path.Remove the redundant
omega=0default 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:
b43425beb2c6ef0c94627708c1602bb96eec8fb5Verified 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) | PassedFinal result: All 12 checks passed, with zero failures and zero pending checks.
Scope
The changes are limited to:
src/datajoint/declare.pytests/schema_university.pytests/unit/test_primary_key_defaults.pyNo new dependencies were introduced and no tests were disabled.
The branch is up to date with upstream
master.Ready for review.