Skip to content

fix(models): writes through loaded instances use keys stored in another form - #138

Merged
kalaspuff merged 3 commits into
fctl/133-work-through-open-issues-and-open-a-pr-sfrom
issues-stack/2-stored-keys
Oct 31, 2026
Merged

kalaspuff merged 3 commits into
fctl/133-work-through-open-issues-and-open-a-pr-sfrom
issues-stack/2-stored-keys

Conversation

@fctl-sh

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

Copy link
Copy Markdown
Contributor

Summary

A key that another writer stored in a form that decodes to the same values, but that dynamoxy does not write (an upper-case UUID, a date as 20260102, also inside a key template such as DEVICE#{device_id}), made its loaded instances address no item: they render the canonical form, so save(), delete() and update() through them raised "the key of this instance changed", and primary_key() returned the key of no item. ADR 0015 says keys of loaded instances are the stored keys.

Writes through a loaded instance (puts, saves, updates, deletes, batches and transactions) and its primary_key() now use the key as stored when the instance's key fields still decode to the same values; a key field that really changed is refused as before. Immutable fields had the same problem (save() raised "is immutable" for a value stored that way) and keep their stored form too. The checks decode stored values only when the rendered form differs from the stored one, so ordinary writes cost nothing more.

An integration test, on the memory backend and MiniStack, stores such a key the way another writer would and saves, refreshes, updates (directly and in a transaction), batch-puts and deletes through the loaded instance, with an immutable date stored the same way; it failed before the fix. uv run pytest, ruff and pyrefly pass. The modeling guide says how such keys behave.

Refs #99

Worth confirming

  • Decision: only key attributes and immutable fields keep their stored form. Other attributes, index keys included, are written in the canonical form on save(), since ADR 0015 documents rewriting a value's format on save as a migration path.

fctl session


Note

Medium risk. It changes which key every write through a loaded model instance (save, update, delete, batches, transactions) sends to DynamoDB, though only when the stored key differs in form from the rendered one.

What it does. When another writer stored a key in a form dynamoxy reads but does not write (an upper-case UUID, a date as 20260102), loaded instances rendered the canonical key, which addresses no item, so save(), update() and delete() failed with "the key of this instance changed". Writes through a loaded instance and primary_key() now use the key as stored when the key fields still decode to the same values, immutable fields keep their stored form on save, and after a write the derived key fields of the instance hold the stored key. A new integration test stores such a key and writes through the loaded instance in every way.

Stored forms kept on write. Only key attributes and immutable fields keep their stored form on save(); other attributes, index keys included, are rewritten in the canonical form.

fctl review's first look at f7634dc · 8 files, +152 −20 · Review agent settings

fctl-sh Bot and others added 2 commits October 31, 2026 04:28
…er form

A key that another writer stored in a form that decodes to the same
values but that dynamoxy does not write (an upper-case UUID, a date as
20260102, also inside a key template) rendered canonically from the
loaded instance, which addresses no item: save(), delete() and update()
raised "the key of this instance changed", and primary_key() returned
the key of no item. Writes through a loaded instance and primary_key()
now use the key as stored, as ADR 0015 says.

Refs #99

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

This comment has been minimized.

Comment thread python/dynamoxy/repository.py Resolved
@fctl-sh

fctl-sh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

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

  1. fix(memory): PartiQL index reads refuse attributes the index does not hold #137 fix(memory): PartiQL index reads refuse attributes the index does not hold
  2. fix(models): writes through loaded instances use keys stored in another form #138 fix(models): writes through loaded instances use keys stored in another form ← this pull request
  3. fix(models): repositories refuse instances of subclasses and items of other models #139 fix(models): repositories refuse instances of subclasses and items of other models
  4. fix(models): documents read from an item keep the keys their class does not declare #140 fix(models): documents read from an item keep the keys their class does not declare
  5. fix(pydantic): key values and update values run the field's pydantic validators #141 fix(pydantic): key values and update values run the field's pydantic validators
  6. fix(repository): updates render derived attributes from the stored item, and require it unchanged #142 fix(repository): updates render derived attributes from the stored item, and require it unchanged
  7. fix(tables): provisioned billing gives global indexes capacity, and table steps wait for the one before #143 fix(tables): provisioned billing gives global indexes capacity, and table steps wait for the one before

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