redis-om-python icon indicating copy to clipboard operation
redis-om-python copied to clipboard

How to replace default primary key?

Open oren-twigo opened this issue 2 years ago • 1 comments

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.

oren-twigo avatar May 14 '22 14:05 oren-twigo

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="[email protected]",
    join_date=datetime.date.today(),
    age=38,
    bio="Python developer, works at Redis, Inc."
)
customer = Customer.get(uuid)

melder avatar Aug 01 '22 22:08 melder

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

sav-norem avatar Sep 09 '22 16:09 sav-norem

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="[email protected]",
    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

edwardzjl avatar Jul 23 '23 13:07 edwardzjl