redis / redis-om-python

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

'NoneType' object is not subscriptable #532

Closed ihormelnykhebronsoft closed 3 months ago

ihormelnykhebronsoft commented 1 year ago
    def parse_command(response, **options):
        commands = {}
        for command in response:
            cmd_dict = {}
>           cmd_name = str_if_bytes(command[0])
E           TypeError: 'NoneType' object is not subscriptable

.venv/lib/python3.10/site-packages/redis/client.py:560: TypeError

-------------

class Task(JsonModel):
    task_id: str = Field(index=True, default="")
    task_tag: str = Field(index=True)
    task_keyword: str = Field(index=True)
    data_json: str = Field(index=False, default="{}")
    task_status: str = Field(index=True)
    task_time: int = Field(index=False, default=0)

When i create a Task (Even did not call any save() stuff). It crashes. I guess something is missing

ihormelnykhebronsoft commented 1 year ago
task = Task(
            task_tag=newTag,
            task_keyword=keyword,
            task_status=STATUS_PEND,
            task_json=json.dumps(income_task),
        )
EnzoPB commented 11 months ago

I'm having the same issue using find() Traceback:

Traceback (most recent call last):
  File "./venv/lib/python3.11/site-packages/redis/client.py", line 1269, in execute_command
    return conn.retry.call_with_retry(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./venv/lib/python3.11/site-packages/redis/retry.py", line 46, in call_with_retry
    return do()
           ^^^^
  File "./venv/lib/python3.11/site-packages/redis/client.py", line 1270, in <lambda>
    lambda: self._send_command_parse_response(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./venv/lib/python3.11/site-packages/redis/client.py", line 1246, in _send_command_parse_response
    return self.parse_response(conn, command_name, **options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./venv/lib/python3.11/site-packages/redis/client.py", line 1296, in parse_response
    return self.response_callbacks[command_name](response, **options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./venv/lib/python3.11/site-packages/redis/client.py", line 560, in parse_command
    cmd_name = str_if_bytes(command[0])
                            ~~~~~~~^^^
TypeError: 'NoneType' object is not subscriptable
python-BaseException

Code:

import redis_om
import datetime
from typing import Optional

class Server(redis_om.HashModel):
    hostname: str
    ip: Optional[str]
    port: int
    created: datetime.datetime

if __name__ == '__main__':
    print(Server.find(Server.hostname == 'xx.xx.xx.xx'))

I'm using:

I don't think the code has a problem, because I get the exam same result by using the code given in the docs.

Praj-17 commented 11 months ago

Hi, Any solution over this, i'm facing the same issue.

EnzoPB commented 11 months ago

@Praj-17 I changed to Redis Stack Server. I think the problem was that I was missing the Redisearch module.

jason810496 commented 9 months ago

I face this problem too, and I found that the problem is that the Redis Server we use is Redis Server instead of Redis Stack Server
While I look into docker-compose.yml file of this OM for Python : Flask and a simple domain model Tutorial. The redis image is redis/redis-stack:latest instead of redis:latest.


Start a Redis Stack Server instead of Redis Server will solve this problem !

- docker run -p 6379:6379 -d redis:lastest
+ docker run -p 6379:6379 -d redis/redis-stack:lastest

The code I use to verify this problem is as follow:

from redis_om import get_redis_connection
from redis_om import JsonModel , HashModel , Migrator , Field
from typing import Optional

REDIS_URL = "redis://localhost:6379"
redis = get_redis_connection(url=REDIS_URL)

class UserJsonCache( JsonModel ):
    id: int = Field(index=True)
    name : str = Field(index=True)
    email: str = Field(index=True)
    avatar:Optional[str] =  Field(index=False)

    class Meta:
        database = redis

class UserHashCache( HashModel ):
    id: int = Field(index=True)
    name : str = Field(index=True)
    email: str = Field(index=True)
    avatar:Optional[str] =  Field(index=False)

    class Meta:
        database = redis

Migrator().run()

def test_create_user_json():
    new_user = UserJsonCache(id=1,name="json_user",email="json_user@email.com",avatar="image_url")
    new_user.save()
    pk = new_user.pk
    assert UserJsonCache.get(pk) == new_user

def test_find_user_json():
    user_be_found = UserJsonCache(id=1,name="json_user",email="json_user@email.com",avatar="image_url")
    res = UserJsonCache.find( UserJsonCache.id==1 ).first()

    assert res.id == user_be_found.id
    assert res.name == user_be_found.name
    assert res.email == user_be_found.email
    assert res.avatar == user_be_found.avatar
    '''
    We can't use assert `res == user_be_found` because `pk` is different
    '''

def test_create_user_hash():
    new_user = UserHashCache(id=2,name="hash_user",email="hash_user@email.com",avatar="image_url")
    new_user.save()
    pk = new_user.pk
    assert UserHashCache.get(pk) == new_user

def test_find_user_hash():
    user_be_found = UserHashCache(id=2,name="hash_user",email="hash_user@email.com",avatar="image_url")
    res = UserHashCache.find( UserHashCache.id==2 ).first()
    assert res.id == user_be_found.id
    assert res.name == user_be_found.name
    assert res.email == user_be_found.email
    assert res.avatar == user_be_found.avatar

Run well no matter using HashModel or JsonModel 🙌

pytest result

pytest result

RedisInsight

redis insight
kubi-ozisik commented 8 months ago

@jason810496 By default the RedisJSON module is not being loaded and when you try to run a json command against the local instance, you see image this. However, I have loaded the modules with command: redis-server --loadmodule /opt/redis-stack/lib/rejson.so --loadmodule /opt/redis-stack/lib/redisearch.so --loadmodule /opt/redis-stack/lib/redistimeseries.so this command and still getting the same error. Which redis-stack image are you using?

slorello89 commented 3 months ago

Going to close to tidy up (pretty clearly a redis/module version issues)