Skip to content
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

How to replace default primary key? #251

Closed
oren-twigo opened this issue May 14, 2022 · 3 comments
Closed

How to replace default primary key? #251

oren-twigo opened this issue May 14, 2022 · 3 comments

Comments

@oren-twigo
Copy link

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 Bump actions/checkout from 2.3.4 to 2.3.5 #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
Copy link
Contributor

melder commented Aug 1, 2022

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)

@sav-norem
Copy link

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

@edwardzjl
Copy link

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

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

No branches or pull requests

5 participants