Skip to content

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

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

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

Conversation

@godara-alt

@godara-alt godara-alt commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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.

…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
@godara-alt

Copy link
Copy Markdown
Contributor Author

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.

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!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants