pynamodb / PynamoDB

A pythonic interface to Amazon's DynamoDB
http://pynamodb.readthedocs.io
MIT License
2.46k stars 427 forks source link

ValueError: range_key = {'S': 'transactions'} is not a valid range key condition #564

Open grvhi opened 5 years ago

grvhi commented 5 years ago

When querying a global secondary index, I cannot apply both a range_key_condition and filter_expression to the query:

This code works exactly as expected:

return SomeModel.some_index.query(
                'some-hash-id',
                range_key_condition=cls.range_key == 'some-value',
)

But this code fails with a ValueError:

return SomeModel.some_index.query(
                'some-hash-id',
                range_key_condition=cls.range_key == 'some-value',
                filter_condition=cls.status == 0
)

Note that the status attribute is a NumberAttribute and is not a hash key or range key. And cls.range_key is the attribute name of the model's range key and is a UnicodeAttribute.

The exception is:

  File "/Users/g/.local/share/virtualenvs/kJlAiHk8/lib/python3.6/site-packages/pynamodb/pagination.py", line 173, in __next__
    self._get_next_page()
  File "/Users/g/.local/share/virtualenvs/kJlAiHk8/lib/python3.6/site-packages/pynamodb/pagination.py", line 158, in _get_next_page
    page = next(self.page_iter)
  File "/Users/g/.local/share/virtualenvs/kJlAiHk8/lib/python3.6/site-packages/pynamodb/pagination.py", line 103, in __next__
    page = self._operation(*self._args, **self._kwargs)
  File "/Users/g/.local/share/virtualenvs/kJlAiHk8/lib/python3.6/site-packages/pynamodb/connection/table.py", line 257, in query
    select=select)
  File "/Users/g/.local/share/virtualenvs/kJlAiHk8/lib/python3.6/site-packages/pynamodb/connection/base.py", line 1349, in query
    raise ValueError("{0} is not a valid range key condition".format(range_key_condition))
ValueError: range_key = {'S': 'transactions'} is not a valid range key condition
bhayani commented 4 years ago

Were you ever able to figure this out @grvhi? I'm running into the same issue :/

grvhi commented 4 years ago

@bhayani - not that I remember I’m afraid. IIRC, I ended up not using a secondary index anyway, but the issue remained.

ikonst commented 4 years ago

When you don't provide a filter_condition, there's code that gracefully assumes you provided a filter condition in place of a range key condition, and treats your range key condition as a filter condition.

However, if you provide both, then your range key condition must be a proper range key condition.

In the code above:

return SomeModel.some_index.query(
  'some-hash-id',
  range_key_condition=cls.range_key == 'some-value',
  filter_condition=cls.status == 0
)

it's hard to tell just from this code what's wrong, but for starters: 1) Why cls.range_key and not SomeModel.range_key? We're not told what cls is, and I cannot infer if there was a mistake (maybe cls != SomeModel?) 2) I'd need to see the model definition.

What it does tell me is that we need a better error message.