Skip to content

Commit 48a092b

Browse files
committed
fix inheritance from indexed model
1 parent b45ffd5 commit 48a092b

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

aredis_om/model/model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,8 @@ class RedisModel(BaseModel, abc.ABC, metaclass=ModelMeta):
13941394
model_config = ConfigDict(from_attributes=True)
13951395

13961396
def __init__(__pydantic_self__, **data: Any) -> None:
1397-
__pydantic_self__.validate_primary_key()
1397+
if __pydantic_self__.model_config.get("index") is True:
1398+
__pydantic_self__.validate_primary_key()
13981399
super().__init__(**data)
13991400

14001401
def __lt__(self, other):

tests/test_hash_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ class Address(m.BaseHashModel):
754754

755755
@py_test_mark_asyncio
756756
async def test_primary_key_model_error(m):
757-
class Customer(m.BaseHashModel):
757+
class Customer(m.BaseHashModel, index=True):
758758
id: int = Field(primary_key=True, index=True)
759759
first_name: str = Field(primary_key=True, index=True)
760760
last_name: str
@@ -775,13 +775,13 @@ class Customer(m.BaseHashModel):
775775

776776
@py_test_mark_asyncio
777777
async def test_primary_pk_exists(m):
778-
class Customer1(m.BaseHashModel):
778+
class Customer1(m.BaseHashModel, index=True):
779779
id: int
780780
first_name: str
781781
last_name: str
782782
bio: Optional[str]
783783

784-
class Customer2(m.BaseHashModel):
784+
class Customer2(m.BaseHashModel, index=True):
785785
id: int = Field(primary_key=True, index=True)
786786
first_name: str
787787
last_name: str

tests/test_json_model.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,10 @@ class Child(Model):
13291329
pass
13301330

13311331
assert issubclass(Child, Model)
1332+
1333+
child = Child(name="John")
1334+
1335+
assert child.name == "John"
13321336

13331337

13341338
@py_test_mark_asyncio

0 commit comments

Comments
 (0)