Skip to content

Preserve primary key when loading models#842

Open
sakshichitnis27 wants to merge 2 commits into
redis:mainfrom
sakshichitnis27:fix/828-preserve-loaded-primary-key
Open

Preserve primary key when loading models#842
sakshichitnis27 wants to merge 2 commits into
redis:mainfrom
sakshichitnis27:fix/828-preserve-loaded-primary-key

Conversation

@sakshichitnis27

@sakshichitnis27 sakshichitnis27 commented Jul 11, 2026

Copy link
Copy Markdown

Fixes #828

Summary

Preserve the requested primary key when loading HashModel and JsonModel records with a missing stored primary key.

Added tests for default and custom primary keys.

Testing

  • pytest -q tests/test_primary_key_loading.py tests_sync/test_primary_key_loading.py

Note

Low Risk
Narrow change to get() hydration only; uses the same pk already passed to the API and is covered by targeted tests. Stored PK values in the document, if present, would be overwritten by the requested key—intended for legacy missing-PK data.

Overview
HashModel.get and JsonModel.get now set the primary key from the lookup argument when Redis returns a document that omits the PK field (e.g. legacy hashes/JSON without pk stored).

Before validation, the loaders merge {cls._meta.primary_key.name: pk} into the fetched payload on both the normal and byte-decode paths for hashes, and on the JSON path for JsonModel. Custom primary keys (e.g. external_id) use the configured field name, so .pk and the custom field stay aligned with the key used in get(pk).

New async tests in tests/test_primary_key_loading.py cover missing-PK hash/JSON documents, undecoded hash bytes, and a custom Field(primary_key=True).

Reviewed by Cursor Bugbot for commit bc45387. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit eea94cf. Configure here.

Comment thread aredis_om/model/model.py Outdated
Comment thread tests/test_primary_key_loading.py
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.

Primary key (pk) is incorrectly generated during model loading, causing identity inconsistency

1 participant