Skip to content

Preserve primary key when loading models#842

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

Preserve primary key when loading models#842
sakshichitnis27 wants to merge 1 commit 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
Small, read-path change in get() with focused tests; behavior only affects records missing a stored PK and should not alter keys that already include one.

Overview
HashModel.get and JsonModel.get now inject the lookup key into the document before Pydantic validation when Redis returns a record without a stored primary key field (common for legacy data).

The primary key field name comes from cls._meta.primary_key.name, so default pk and custom Field(primary_key=True) fields both resolve correctly. The HashModel byte-decode fallback path sets the same field after decoding.

New async tests cover missing stored PK for hash and JSON models, bytes responses, and a custom primary key.

Reviewed by Cursor Bugbot for commit 010fa7b. 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
@sakshichitnis27 sakshichitnis27 force-pushed the fix/828-preserve-loaded-primary-key branch from bc45387 to 010fa7b Compare July 12, 2026 05:58
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