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

find fails on a JsonModel when querying more than one HashModel field using two or more expressions #357

Open
wiseaidev opened this issue Aug 20, 2022 · 4 comments

Comments

@wiseaidev
Copy link
Contributor

wiseaidev commented Aug 20, 2022

Code to reproduce the issue:

from typing import Optional, Any

from fastapi import FastAPI

from pydantic import BaseModel, Field as PydanticField, EmailStr
import datetime

from aredis_om import (
    Field,
    HashModel,
    JsonModel,
    Migrator,
    get_redis_connection
)

redis_conn = get_redis_connection(
    url=f"redis://localhost:6379",
    decode_responses=True
)

class User(HashModel):
    first_name: Optional[str] = Field(index=True)
    last_name: Optional[str] = Field(index=True)
    email: EmailStr = Field(index=True)
    password: str = Field(index=True)
    created_on: Optional[datetime.datetime] = Field(default_factory=datetime.datetime.now)

    class Meta:
        database = redis_conn

class Contact(JsonModel):
    user: User = Field(index=True)
    contact: User = Field(index=True)
    message: Optional[str] = Field(index=True, default="yo")
    created_on: Optional[datetime.datetime] = Field(default_factory=datetime.datetime.now)
    class Meta:
        database = redis_conn

router = FastAPI(title=__name__)

@router.on_event("startup")
async def startup():
    await Migrator().run()
    user1 = await User(email="[email protected]", password="S3C11R3P@ssW0rD").save()
    user2 = await User(email="[email protected]", password="P@ssW0rD").save()
    contact1 = await Contact(user=user1, contact=user2).save()
    contact2 = await Contact.find((Contact.user.email == "[email protected]") & (Contact.contact.email == "[email protected]")).all()
    print(contact2)

However, the following expressions work as expected:

    contact2 = await Contact.find((Contact.user.email == "[email protected]") & (Contact.message == "yo")).all()
    contact2 = await Contact.find(Contact.user.email == "[email protected]").all()

This confirms my hypothesis that a JsonModel object can index at most one HashModel field. Is that intentional or a bug?

@DasLukas
Copy link

DasLukas commented Aug 28, 2022

Have you try EmbeddedJsonModel instead of HashModel?

@mrkprdo
Copy link

mrkprdo commented Sep 30, 2022

@wiseaidev Does this line work?
await Migrator().run()

@tihmels
Copy link

tihmels commented Dec 2, 2022

Try using and instead of &

@XChikuX
Copy link

XChikuX commented Jan 15, 2023

@wiseaidev Does this line work? await Migrator().run()

I've tried this in my asynchronous code. It does seem to work.
I'm not sure what happens under the hood though.
There doesn't seem to be much documentation to how exactly this helps redis-om 'create indices' in redis.

@wiseaidev I've opened up this issue to track a related 'potential' bug: #462

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants