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

enhancing primary key check to be compatible with sqlmodel #672

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading