You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/models.md
+70Lines changed: 70 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -127,6 +127,76 @@ Here is a table of the settings available in the Meta object and what they contr
127
127
| 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" |
128
128
| 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 |
129
129
| 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
+
classUUIDv7PrimaryKey:
146
+
@staticmethod
147
+
defcreate_pk(*args, **kwargs) -> str:
148
+
returnstr(uuid.uuid7())
149
+
150
+
classMyModel(HashModel):
151
+
name: str
152
+
153
+
classMeta:
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
+
classCustomPrimaryKey:
165
+
@staticmethod
166
+
defcreate_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 importABC
177
+
import uuid
178
+
from redis_om import HashModel
179
+
180
+
classUUIDv7PrimaryKey:
181
+
@staticmethod
182
+
defcreate_pk(*args, **kwargs) -> str:
183
+
returnstr(uuid.uuid7())
184
+
185
+
classBaseModel(HashModel, ABC):
186
+
classMeta:
187
+
primary_key_creator_cls = UUIDv7PrimaryKey
188
+
189
+
classCustomer(BaseModel):
190
+
name: str
191
+
email: str
192
+
193
+
classOrder(BaseModel):
194
+
product: str
195
+
quantity: int
196
+
197
+
# Both Customer and Order will use UUID v7 for primary keys
198
+
```
199
+
130
200
## Configuring Pydantic
131
201
132
202
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