redis / redis-om-python

Object mapping, and more, for Redis and Python
MIT License
1.12k stars 112 forks source link

How to replace default primary key? #251

Closed oren-twigo closed 2 years ago

oren-twigo commented 2 years ago

I have two use cases in which I would like to replace the default pk field with another field:

  1. I would like to define that a string field named "uuid" to be my primary key field, instead of the default "pk". I do want to keep the ULID key generator, but just to set it into a field named "uuid" instead of "pk".

  2. same as #1, just skipping the ULID key generator, allowing me to set the primary key manually.

Is it possible to achieve? I would like to achieve it for both Json and Hash models. The documentation on that aspect is not clear enough.

Thanks in advance for anyone who will help.

melder commented 2 years ago

Using the readme example:

import datetime
from typing import Optional

from pydantic import EmailStr

from redis_om import HashModel

class Customer(HashModel):
    first_name: str
    last_name: str
    email: EmailStr
    join_date: datetime.date
    age: int
    bio: Optional[str]
uuid= "46b8d309-c085-4b52-b88c-7c906a8e6cea"
andrew = Customer(
    pk= uuid,
    first_name="Andrew",
    last_name="Brookins",
    email="andrew.brookins@example.com",
    join_date=datetime.date.today(),
    age=38,
    bio="Python developer, works at Redis, Inc."
)
customer = Customer.get(uuid)
sav-norem commented 2 years ago

@simonprickett seems like this has been addressed and should be closed

edwardzjl commented 1 year ago

Using the readme example:

import datetime
from typing import Optional

from pydantic import EmailStr

from redis_om import HashModel

class Customer(HashModel):
    first_name: str
    last_name: str
    email: EmailStr
    join_date: datetime.date
    age: int
    bio: Optional[str]
uuid= "46b8d309-c085-4b52-b88c-7c906a8e6cea"
andrew = Customer(
    pk= uuid,
    first_name="Andrew",
    last_name="Brookins",
    email="andrew.brookins@example.com",
    join_date=datetime.date.today(),
    age=38,
    bio="Python developer, works at Redis, Inc."
)
customer = Customer.get(uuid)

This solves question 2 but not question 1

The field name is still pk