Repository navigation
fix(declare): reject constant defaults on primary-key attributes - #1759
godara-alt wants to merge 6 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
left a comment
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.
Thanks for the detailed review. I've addressed both regressions. The primary-key default check now applies only to new declarations, without affecting alter() or lineage refresh on existing tables. I've also added integration tests for the reported cases on both backends, covered CURRENT_TIMESTAMP(3), and updated the error message as suggested. All 12 CI checks are passing, including lint, MySQL 8.0/8.4, Windows, and minimum dependencies. The changes are pushed to the same PR. Please take another look when you get a chance. Thanks! |
…h PostgreSQL alter support
The issue was that constant defaults were allowed on primary-key attributes, which could cause multiple insertions to use the same primary-key value when a key was not explicitly provided.
Following the maintainer's review, the implementation has been updated to ensure that the validation only applies to new table declarations and does not affect existing tables.
Both reported regressions related to alter() and automatic lineage refresh have been addressed. Existing tables with legacy primary-key defaults can still be altered, and missing lineage records can be repaired without being affected by the new validation.
The error message has also been updated to identify the affected attribute and explain the available alternatives. Generated CURRENT_TIMESTAMP defaults, secondary attribute defaults and the internal singleton key continue to work as expected.
The branch has been synchronized with upstream master, including all 211 upstream commits. Merge conflicts have been resolved while preserving the PostgreSQL alter implementation introduced in #1639.
Testing and Verification
The regression tests cover constant primary-key defaults, existing table alterations, automatic lineage repair, singleton behavior and generated timestamp defaults on both MySQL and PostgreSQL.
The existing table regression test has also been improved to reload the table heading after changing the primary-key default directly in the database. This ensures that the stored default is actually read before performing the alteration.
The complete CI verification was performed on the updated commit, and all 12 jobs passed successfully.
Linux unit tests: 1,940 passed.
Primary-key regression unit tests: 24 passed.
MySQL 8.0 and 8.4 integration tests passed.
PostgreSQL integration tests passed.
Python 3.10, 3.14 and 3.15 compatibility verified.
Windows unit tests passed across all three Python versions.
Minimum dependency compatibility verified.
Lint and formatting checks passed.
Each of the seven full-suite jobs completed with 3,385 passed tests and 19 skipped tests.
CI Results
Verified commit: ae28dec
Full test results:
https://github.com/godara-alt/datajoint-python/actions/runs/47096213981
Lint verification:
https://github.com/godara-alt/datajoint-python/actions/runs/47096213915
All 12 fork CI jobs passed on the exact commit currently submitted in this PR. Upstream CI is tracked separately.
No new dependencies have been introduced, no tests have been disabled, and no CI workflow modifications have been made.
The changes are aligned with the v2.4 milestone as suggested during the review.
Thanks for the detailed review.