Skip to content

fix(unique)!: upserts that race with creates, unique floats written back, and collections refused - #129

Merged
kalaspuff merged 2 commits into
issues-stack/3-pydantic-modelsfrom
issues-stack/4-unique-guards
Oct 31, 2026
Merged

kalaspuff merged 2 commits into
issues-stack/3-pydantic-modelsfrom
issues-stack/4-unique-guards

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Three ways unique guards got out of step with their items (#82):

  • Upserts that race with creates: an upsert that set a unique value read the item and, finding none, wrote without requiring that it was still missing (put() already adds partition.not_exists() in this case). When another writer created the item between the read and the write, the upsert overwrote its unique value and never deleted the other writer's guard, which stayed taken forever. The upsert now requires that the item is still missing. When that fails because an item appeared, it reads the item again and writes once more, freeing the replaced value's guard. This is the replan that conditional deletes got in fix(repository): unique models check delete conditions on missing items, and stale deletes and updates read again #110. In a transaction it cancels with a ConditionalCheckFailedError that says another writer stored the item and to retry. Upserts of stale loaded instances still raise as before. The same condition covers upserts that read the item to render a derived attribute (Player.score.add(1) with rank="SCORE#{score:08d}"): they wrote a rank rendered from the missing item over the created one (score 6, rank SCORE#00000001), and now render it from the created item.
  • Unique floats could not be written back: _plan_guards compared the stored Decimal("0.1") with the float 0.1 using ==, so the plan deleted and put the same guard item, which DynamoDB refuses. save(), put() and update(Place.latitude.set(0.1)) of an instance read back failed with the value unchanged. Guards now compare stored identities, as keys already do.
  • Collections were accepted as unique: the guard key is the value's text, and a set's text depends on the process's hash seed. unique=True on a set, list, map or document field is now a TypeError when the model's schema is built, and a collection stored in a unique Any field raises ValueError before any write.

Verified with integration tests on the in-memory backend and MiniStack, each failing without the fix: a middleware that creates the item right after the upsert's GetItem (for a unique value, standalone and in a transaction, and for a derived rank), a float field saved, put and updated after a read, and the refused collection fields and values. make check passes.

Fixes #82

Breaking change

unique=True on a set, list, map or document field now raises TypeError when the model is used. Such guards never had one text per value, so they did not guarantee uniqueness. Drop unique=True from the field, or keep a canonical string of the value (",".join(sorted(tags))) in a unique field of its own.

Checklist

  • Tests cover the change (in-memory backend and MiniStack)
  • Docs updated (constraints guide, AGENT_GUIDE, changelog)
  • make check passes locally (lint, types, clippy, consistency, tests)
  • Breaking change? Marked with ! in the title and explained above

Worth confirming

  • Decision: collections are refused rather than given a canonical text, as the issue offered both. The refusal is a TypeError for declared collection types and a ValueError for collection values of Any fields; booleans stay allowed.
  • Decision: an upsert that loses the race to a create is planned again from the created item, so it updates that item and replaces its unique value, instead of failing.

fctl session


Note

Medium risk. It changes how upserts and writes of models with unique fields plan their conditions and guard items, which every such write goes through, and it makes models that declare unique collection fields fail where they were accepted before.

What it does. Fixes three ways unique guard items could get out of step with their items. An upsert that finds no item now requires that the item is still missing, and when another writer created it in the meantime, it reads the item again and writes once more, so the replaced value's guard is freed and derived keys such as rank are rendered from the created item. Guards of unique floats are compared as stored values, so a float read back no longer deletes and puts the same guard item. Unique set, list and map fields are refused with a TypeError when the schema is built, and collection values in unique Any fields raise a ValueError before any write.

Breaking change. Models that declare unique=True on a set, list, map or document field now raise a TypeError as soon as their schema is used, so applications with such models stop working until they remove the option or keep a canonical string in a unique field of its own.

fctl review's first look at 3ba79de · 8 files, +185 −12 · Review agent settings

…ck, and collections refused

An upsert that set a unique value and found no item added no condition
that the item was still missing, so an item created meanwhile lost its
value while its guard stayed taken forever. It now requires that, and
is planned again from the stored item when it fails because of such a
create. Guards compared stored and new values with ==, so a float read
back as Decimal deleted and put the same guard item; they now compare
stored identities. unique=True on sets, lists and maps (whose guard
text has no canonical form) is refused, and collections in unique Any
fields raise ValueError before any write.

Fixes #82

Co-authored-by: Carl Oscar Aaro <[email protected]>
@fctl-review

This comment has been minimized.

Comment thread python/dynamoxy/repository.py Resolved
Comment thread CHANGELOG.md Resolved
… a create

The missing-item condition also covers upserts that read the item to
render a derived attribute; test it, and say so in the comment and the
changelog. The refusal of unique collections moves to Changed as a
breaking change.

BREAKING CHANGE: unique=True on set, list, map and document fields is a
TypeError when the model is used.

Co-authored-by: Carl Oscar Aaro <[email protected]>
@fctl-sh fctl-sh Bot changed the title fix(unique): upserts that race with creates, unique floats written back, and collections refused fix(unique)!: upserts that race with creates, unique floats written back, and collections refused Oct 31, 2026
@kalaspuff
kalaspuff added this pull request to stack #136 October 31, 2026 03:58
@fctl-sh

fctl-sh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Stack of 10 pull requests, merged in this order with "Squash and merge":

  1. fix(models): numbers of union fields render as stored, and literals of plain enum members write #126 fix(models): numbers of union fields render as stored, and literals of plain enum members write
  2. fix(client): calls keep their middleware chain and deliver all their events #127 fix(client): calls keep their middleware chain and deliver all their events
  3. fix(pydantic): use_enum_values, forward references, RootModel fields and data-taking factories work #128 fix(pydantic): use_enum_values, forward references, RootModel fields and data-taking factories work
  4. fix(unique)!: upserts that race with creates, unique floats written back, and collections refused #129 fix(unique)!: upserts that race with creates, unique floats written back, and collections refused ← this pull request
  5. fix(transactions): commit() no longer raises after the transaction was applied #130 fix(transactions): commit() no longer raises after the transaction was applied
  6. fix(telemetry): records and metrics count PartiQL writes, throttles of failed calls, batch retries and rows #131 fix(telemetry): records and metrics count PartiQL writes, throttles of failed calls, batch retries and rows
  7. fix(testing): purge_expired, inject_fault(table=) and replayed transactions behave like DynamoDB #132 fix(testing): purge_expired, inject_fault(table=) and replayed transactions behave like DynamoDB
  8. fix(memory): queries refuse empty key values and start keys outside the key condition, and index keys have size limits #133 fix(memory): queries refuse empty key values and start keys outside the key condition, and index keys have size limits
  9. fix(binding): conversions take any mapping and refuse invalid wire values, and unknown table statuses are not active #134 fix(binding): conversions take any mapping and refuse invalid wire values, and unknown table statuses are not active
  10. fix(memory): PartiQL and expressions follow DynamoDB's rules, and hand-written conditions get no redundant parentheses #135 fix(memory): PartiQL and expressions follow DynamoDB's rules, and hand-written conditions get no redundant parentheses

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.

Unique guards: upserts race with creates, floats cannot be written back, and collections are accepted

1 participant