lyft / pynamodb-attributes

Common attributes for PynamoDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`range_key_condition` Doesn't work with TimestampMSAttribute

Seluj78 opened this issue · comments

The table:

class EventTypeIndex(GlobalSecondaryIndex):
    class Meta:
        index_name = "EventTypeIndex"
        projection = AllProjection()

    event_type = UnicodeEnumAttribute(
        LogEventTypeEnum,
        hash_key=True,
        unknown_value=LogEventTypeEnum.GENERIC,
        default_for_new=LogEventTypeEnum.GENERIC,
    )


class Logs(Model):
    class Meta:
        table_name = "Logs"
        region = Session().get_config_variable("region")
        billing_mode = PAY_PER_REQUEST_BILLING_MODE

    user_id = NumberAttribute(hash_key=True)
    timestamp = TimestampMsAttribute(range_key=True)

    event_type = UnicodeEnumAttribute(
        LogEventTypeEnum, unknown_value=LogEventTypeEnum.GENERIC, default_for_new=LogEventTypeEnum.GENERIC
    )
    event_type_index = EventTypeIndex()
    content = UnicodeAttribute(null=True)

The crash :

[i for i in Logs.query(hash_key=9152, range_key_condition=Logs.timestamp.between(0, 9999999999999999))]
Traceback (most recent call last):
  File "/Users/seluj78/Applications/PyCharm Professional Edition.app/Contents/plugins/python/helpers/pydev/pydevconsole.py", line 364, in runcode
    coro = func()
           ^^^^^^
  File "<input>", line 1, in <module>
  File "/Users/seluj78/Projects/Datascientest/hub-backend/.venv/lib/python3.11/site-packages/pynamodb/attributes.py", line 176, in between
    return Path(self).between(lower, upper)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/seluj78/Projects/Datascientest/hub-backend/.venv/lib/python3.11/site-packages/pynamodb/expressions/operand.py", line 79, in between
    return Between(self, self._to_operand(lower), self._to_operand(upper))
                         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/seluj78/Projects/Datascientest/hub-backend/.venv/lib/python3.11/site-packages/pynamodb/expressions/operand.py", line 45, in _to_operand
    return Path(value) if isinstance(value, Attribute) else self._to_value(value)
                                                            ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/seluj78/Projects/Datascientest/hub-backend/.venv/lib/python3.11/site-packages/pynamodb/expressions/operand.py", line 328, in _to_value
    return Value(value, attribute=self.attribute)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/seluj78/Projects/Datascientest/hub-backend/.venv/lib/python3.11/site-packages/pynamodb/expressions/operand.py", line 200, in __init__
    (self.attr_type, value) = Value.__serialize(value, attribute)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/seluj78/Projects/Datascientest/hub-backend/.venv/lib/python3.11/site-packages/pynamodb/expressions/operand.py", line 221, in __serialize
    return attribute.attr_type, attribute.serialize(value)
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/seluj78/Projects/Datascientest/hub-backend/.venv/lib/python3.11/site-packages/pynamodb_attributes/timestamp.py", line 26, in serialize
    return str(int(value.timestamp() * self._multiplier))
                   ^^^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'timestamp'