redis / redis-om-python

Object mapping, and more, for Redis and Python
MIT License
1.09k stars 108 forks source link

find() is throwing index does not exist error when imported in a different file #459

Closed XChikuX closed 1 year ago

XChikuX commented 1 year ago

I tried the following:

redis_test.py

import datetime
from pydantic import BaseModel, EmailStr, ValidationError

from redis_om import (
    Field,
    HashModel,
    Migrator,
    get_redis_connection
)

# Redis
redis_conn = get_redis_connection(
    url=f"redis://10.9.9.4:6379",
    decode_responses=True,
    password="Withheld"
)

class Customer(HashModel):
    first_name: str
    last_name: str = Field(index=True)
    email: EmailStr
    join_date: datetime.date
    age: int = Field(index=True)
    bio: str
    class Meta:
        database = redis_conn

print(Customer.find(Customer.last_name == 'Testing').all())

Output: []

Since I had no records of customer inserted an empty list is expected.

However, when I try the same command from a different python file testing.py

from redis_test import Customer

print(Customer.find(Customer.last_name == 'Testing').all())

I get the follow error: redis.exceptions.ResponseError: :redis_test.Customer:index: no such index

I believe this is a bug.

EDIT: Hold on!! I found something more interesting The sync version errors out during import. Thus, both get and find calls fail. The async version only errors out on find() and Customer.get() works with no issue. Weird

simonprickett commented 1 year ago

This might be down to your use of providing a database connection in meta... importing the model and calling find should work fine (see here for an example) https://github.com/redis-developer/redis-om-python-flask-skeleton-app

how's your connection getting established in your application where this doesn't work?

simonprickett commented 1 year ago

Adding Discord discussion link: https://discord.com/channels/697882427875393627/958455824278245406/1061960759090892830

XChikuX commented 1 year ago

To add on to what @simonprickett said. providing a database connection in meta

This causes the following symptoms:

1) The sync version errors out during import. Thus, both get and find calls fail. 2) The async version only errors out on find(), while Customer.get() works with no issue. Weird

Could we please get this looked at soon? Seems like a fundamental issue that is completely blocking my future progress.

simonprickett commented 1 year ago

I guess the other follow up question I have here is which file and when do you run the Migrator?

XChikuX commented 1 year ago

Currently the issue persists whether or not I run the Migrator. But I ran it in an async main function after all the model definitions with a asyncio.sleep(0)

XChikuX commented 1 year ago

This boiled down to calling Migrator in the right location. Which is the file where the Model is being used. Closing, Sorry about the confusion guys.