Skip to content

Allow users to define a new primary key. #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
get primary key field using the key method
Signed-off-by: wiseaidev <business@wiseai.dev>
  • Loading branch information
wiseaidev committed Aug 11, 2022
commit 2be329f66d49d062d4cf2b4447cb5127b1a85fc5
6 changes: 3 additions & 3 deletions aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ def __init__(__pydantic_self__, **data: Any) -> None:

def __lt__(self, other):
"""Default sort: compare primary key of models."""
return self.pk < other.pk
return self.key() < other.key()

def key(self):
"""Return the Redis key for this model."""
Expand Down Expand Up @@ -1149,7 +1149,7 @@ async def expire(
db = self._get_db(pipeline)

# TODO: Wrap any Redis response errors in a custom exception?
await db.expire(self.make_primary_key(self.pk), num_seconds)
await db.expire(self.make_primary_key(self.key()), num_seconds)

@validator("pk", always=True, allow_reuse=True)
def validate_pk(cls, v):
Expand Down Expand Up @@ -1274,7 +1274,7 @@ async def delete_many(
db = cls._get_db(pipeline)

for chunk in ichunked(models, 100):
pks = [cls.make_primary_key(model.pk) for model in chunk]
pks = [cls.make_primary_key(model.key()) for model in chunk]
await cls._delete(db, *pks)

return len(models)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_hash_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ async def test_delete_many(m):
result = await m.Member.delete_many(members)
assert result == 2
with pytest.raises(NotFoundError):
await m.Member.get(pk=member1.id)
await m.Member.get(pk=member1.key())


@py_test_mark_asyncio
Expand Down