Conversation
Co-authored-by: yyyyyyyan <24644216+yyyyyyyan@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] SEP-2011: Teach duplicate-check precheck to read unique constraints
SEP-2011: Teach the duplicate-check precheck to read unique constraints
Sep 16, 2026
yyyyyyyan
marked this pull request as ready for review
September 16, 2026 20:59
yyyyyyyan
requested review from
a team,
marcuscruz-percona and
peter-o-addo
as code owners
September 16, 2026 20:59
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The shared behavior change leaves stale truthiness contracts and needs a complete caller audit.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Expands duplicate prechecks to cover SQLAlchemy unique constraints and valid falsy key values.
Changes:
- Combines unique indexes and constraints in duplicate detection.
- Adds regression coverage for constraint collisions.
- Documents the resulting 409 response behavior.
File summaries
| File | Description |
|---|---|
app/core/db/crud.py |
Expands duplicate-key discovery. |
tests/app/core/db/test_crud.py |
Adds constraint-path tests. |
changelog.d/SEP-2011.fixed.md |
Records the user-facing fix. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+1203
to
+1205
| # Presence, not truthiness: a legitimate 0, "" or False is a | ||
| # filled key member, and skipping it would miss the collision. | ||
| if all(value is not None for value in equal_filters.values()): |
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.
BaseSQLModelManager.save's duplicate precheck introspectedlocal_table.indexesonly, so the four tables that declare uniqueness as aUniqueConstraintin__table_args__(syncentityabsence,taskhistory_log,taskhistory_log_state,atw_incident_execution) fell through to commit and answeredHTTPBadRequestException(400) where every index-declared table answersHTTPConflictException(409).app/core/db/crud.pyIndex.columnsand eachUniqueConstraint.columns— so the body that buildsequal_filters, queries, and raises stays written once and the message format ("<Model> with the same <columns> already exists") is identical either way.PrimaryKeyConstraintis excluded by theisinstancefilter.A naive port would have kept
all(equal_filters.values()), which silently skips any key whose column holds0— i.e.taskhistory_log's most common row, the first chunk of a stream atstart_offset=0. This narrows the skip condition on the pre-existing index path too, so it can only start catching collisions that were previously missed.tests/app/core/db/test_crud.pyConstraintUniqueModel/ConstraintUniqueManagermirroring theCompositeUniqueModelpattern but declaring a single-column and a composite unique key viaUniqueConstraint, with one column that legitimately holds0.TestSaveDuplicatePrecheckfor a single-column collision, a composite collision (a leading-column-only match still saves), a collision whose key member is0, no write emitted before the conflict, and an update to a free value. All four failing cases reproduce the 400 with thecrud.pychange reverted.changelog.d/SEP-2011.fixed.mdmake changelog-add.No schema change: migrating the four declarations to unique
Indexobjects was the alternative, but it needs Alembic migrations across two tracks and does nothing for the nextUniqueConstraint-only table added.