Skip to content

Commit

Permalink
enhancing primary key check to be compatible with sqlmodel (#672)
Browse files Browse the repository at this point in the history
  • Loading branch information
slorello89 authored Jan 17, 2025
1 parent 758d9c7 commit 82406f0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ def validate_primary_key(cls):
else:
field_info = field.field_info

if getattr(field_info, "primary_key", None):
if getattr(field_info, "primary_key", None) is True:
primary_keys += 1
if primary_keys == 0:
raise RedisModelError("You must define a primary key for the model")
Expand Down Expand Up @@ -1823,7 +1823,7 @@ def schema_for_fields(cls):
else:
field_info = field.field_info

if getattr(field_info, "primary_key", None):
if getattr(field_info, "primary_key", None) is True:
if issubclass(_type, str):
redisearch_field = (
f"{name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR}"
Expand Down Expand Up @@ -2018,7 +2018,7 @@ def schema_for_fields(cls):
field_info = field.field_info
else:
field_info = field
if getattr(field_info, "primary_key", None):
if getattr(field_info, "primary_key", None) is True:
if issubclass(_type, str):
redisearch_field = f"$.{name} AS {name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR}"
else:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_json_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,18 @@ class TestLiterals(JsonModel):
assert rematerialized.pk == item.pk



@py_test_mark_asyncio
async def test_two_false_pks():
from pydantic_core import PydanticUndefined as Undefined

class SomeModel(JsonModel):
field1: str = Field(index=True, primary_key=Undefined)
field2: str = Field(index=True, primary_key=Undefined)

SomeModel(field1="foo", field2="bar")

@py_test_mark_asyncio
async def test_child_class_expression_proxy():
# https://github.com/redis/redis-om-python/issues/669 seeing weird issue with child classes initalizing all their undefined members as ExpressionProxies
class Model(JsonModel):
Expand All @@ -1183,6 +1195,7 @@ class Child(Model):
assert rematerialized.age != 19
assert rematerialized.bio is None

@py_test_mark_asyncio
async def test_merged_model_error():
class Player(EmbeddedJsonModel):
username: str = Field(index=True)
Expand Down

0 comments on commit 82406f0

Please sign in to comment.