Skip to content

Commit a9792e7

Browse files
authored
Document custom primary key creators (UUID v7 workaround) (#795)
* Document custom primary key creators (UUID v7 workaround) * Add spelling exceptions for UUID documentation
1 parent 0659ee6 commit a9792e7

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

.github/wordlist.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,7 @@ UnicodeDecodeError
121121
VectorQuery
122122
VectorRangeQuery
123123
embeddings
124+
UUID
125+
lexicographically
126+
Lexicographically
127+
backport

docs/models.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,76 @@ Here is a table of the settings available in the Meta object and what they contr
127127
| index_name | The RediSearch index name to use for this model. Only used if the model is indexed (`index=True` on the model class). | "{global_key_prefix}:{model_key_prefix}:index" |
128128
| embedded | Whether or not this model is "embedded." Embedded models are not included in migrations that create and destroy indexes. Instead, their indexed fields are included in the index for the parent model. **Note**: Only `JsonModel` can have embedded models. | False |
129129
| encoding | The default encoding to use for strings. This encoding is given to redis-py at the connection level. In both cases, Redis OM will decode binary strings from Redis using your chosen encoding. | "utf-8" |
130+
131+
### Custom Primary Key Creators
132+
133+
By default, Redis OM uses ULID (Universally Unique Lexicographically Sortable Identifier) for primary keys. ULIDs are 128-bit identifiers that are lexicographically sortable and contain a timestamp component.
134+
135+
You can customize how primary keys are generated by providing a custom `primary_key_creator_cls` in the Meta object. This is useful when you need a specific ID format, such as UUID v7.
136+
137+
#### Using UUID v7
138+
139+
UUID v7 is a time-ordered UUID that provides similar benefits to ULID with better compatibility with existing UUID infrastructure. Here's how to use it:
140+
141+
```python
142+
import uuid
143+
from redis_om import HashModel
144+
145+
class UUIDv7PrimaryKey:
146+
@staticmethod
147+
def create_pk(*args, **kwargs) -> str:
148+
return str(uuid.uuid7())
149+
150+
class MyModel(HashModel):
151+
name: str
152+
153+
class Meta:
154+
primary_key_creator_cls = UUIDv7PrimaryKey
155+
```
156+
157+
**Note:** `uuid.uuid7()` requires Python 3.11+ or a backport library like `uuid6`.
158+
159+
#### Custom Primary Key Protocol
160+
161+
Your custom primary key creator class must implement a `create_pk` static method that returns a string:
162+
163+
```python
164+
class CustomPrimaryKey:
165+
@staticmethod
166+
def create_pk(*args, **kwargs) -> str:
167+
# Return your custom primary key as a string
168+
return generate_my_custom_id()
169+
```
170+
171+
#### Sharing Primary Key Creators
172+
173+
You can use an abstract base model to share a custom primary key creator across multiple models:
174+
175+
```python
176+
from abc import ABC
177+
import uuid
178+
from redis_om import HashModel
179+
180+
class UUIDv7PrimaryKey:
181+
@staticmethod
182+
def create_pk(*args, **kwargs) -> str:
183+
return str(uuid.uuid7())
184+
185+
class BaseModel(HashModel, ABC):
186+
class Meta:
187+
primary_key_creator_cls = UUIDv7PrimaryKey
188+
189+
class Customer(BaseModel):
190+
name: str
191+
email: str
192+
193+
class Order(BaseModel):
194+
product: str
195+
quantity: int
196+
197+
# Both Customer and Order will use UUID v7 for primary keys
198+
```
199+
130200
## Configuring Pydantic
131201

132202
Every Redis OM model is also a Pydantic model, so in addition to configuring Redis OM behavior with the Meta object, you can control Pydantic configuration via the Config object within a model class.

0 commit comments

Comments
 (0)