Skip to content

Unique fields: enforce @EntityField.unique() on references and camelCase fields in memory repositories (v1.35.2) - #211

Merged
gmpassos merged 3 commits into
masterfrom
fix/memory-unique-reference-field
Nov 16, 2026
Merged

gmpassos merged 3 commits into
masterfrom
fix/memory-unique-reference-field

Conversation

@gmpassos

@gmpassos gmpassos commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Problem

DBSQLMemoryAdapter never enforced a single-field unique constraint (@EntityField.unique()) on an EntityReference<X> field: two rows referencing the same id were accepted. Postgres enforces the generated UNIQUE constraint (e.g. deliverer_withdrawal__pending_wallet__unique), so in-memory tests diverged from production.

checkEntityFields had two problems:

  • It compared the entity's value from getField (the EntityReference object) with == against the stored value (the referenced id).
  • It read the stored entry by field name only, so camelCase unique fields (stored as external_code) were never enforced either.

Fix

  • DBSQLMemoryAdapter: single-field and composite (@EntityIndex(unique: true)) constraints are now checked in one pass. The entity's values are normalized once, in stored form (a reference becomes its id), and null never conflicts. Each entry is read by field or column name. Values of the same type are compared only by ==, no longer also by toString().
  • SetEntityRepository: a unique EntityReference field compares the referenced id. Before, two null references counted as a duplicate, and a reference set by entity could differ from one set by id.

Heads-up (in the CHANGELOG): in-memory tests that store duplicates of a camelCase unique field now fail, as they would on a SQL database.

Tests

test/bones_api_entity_index_test.dart runs on both DBSQLMemoryAdapter and SetEntityRepository:

  • Unique reference: referenced by entity and by id; different ids and null references are accepted; duplicates are rejected on insert and on update; a row updated with its own reference is accepted; a reference to an unsaved entity is accepted.
  • camelCase String and enum unique fields: duplicates are rejected; nulls are accepted.

The tests fail without the fix. The memory, repository, index and version suites pass, and dart pub publish --dry-run reports 0 warnings.

Version bumped to 1.35.2 (pubspec.yaml, BonesAPI.VERSION, CHANGELOG).

🤖 Generated with Claude Code

gmpassos and others added 2 commits November 15, 2026 21:24
…fields (v1.35.2)

The single-field unique check compared the entity's EntityReference object
with the stored referenced ID, so it never conflicted. It now shares the
composite unique check's comparison (stored form, field or column, null
never conflicts).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- DBSQLMemoryAdapter: single-field and composite unique constraints checked
  in one pass, values normalized once (with the entityName); same-type
  values compared only by ==, not toString().
- SetEntityRepository: a unique EntityReference compares the referenced ID;
  a null reference never conflicts.
- Tests: camelCase String/enum unique fields, an unsaved reference, both
  repositories. CHANGELOG: the camelCase enforcement change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gmpassos gmpassos changed the title DBSQLMemoryAdapter: enforce @EntityField.unique() on EntityReference fields (v1.35.2) Unique fields: enforce @EntityField.unique() on references and camelCase fields in memory repositories (v1.35.2) Nov 16, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.96%. Comparing base (19f1b41) to head (0510cf1).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #211      +/-   ##
==========================================
+ Coverage   95.91%   95.96%   +0.04%     
==========================================
  Files          75       75              
  Lines       25947    25943       -4     
==========================================
+ Hits        24888    24895       +7     
+ Misses       1059     1048      -11     
Flag Coverage Δ
unittests 95.96% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gmpassos
gmpassos merged commit 0463aac into master Nov 16, 2026
11 checks passed
@gmpassos
gmpassos deleted the fix/memory-unique-reference-field branch November 16, 2026 06:08
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.

1 participant