Skip to content

all_pks returns only part of complex primary keys #470

Closed
@YaraslauZhylko

Description

@YaraslauZhylko

I have the following setup (minimized for simplicity):

class Place(JsonModel, ABC):
    id: str = Field(primary_key=True)

    class Meta:
        global_key_prefix = "places"

class Country(Place):
    some_field: int = 1

    class Meta:
        model_key_prefix = "country"

class Province(Place):
    another_field: bool = True

    class Meta:
        model_key_prefix = "province"

class City(Place):
    yet_another_field: str = "bar"

    class Meta:
        model_key_prefix = "city"

And I want id fields (primary keys) to represent some hierarchy n order to be easily identifieable from the first glance.
For that I'd like to use the same : separator that is used for namespacing:

country = Country(id="ca")
province = Province(id="ca:qc")
city = City(id="ca:qc:montreal")

So that the actual Redis keys look like that:

places:country:ca
places:province:ca:qc
places:city:ca:qc:montreal

This setup works for most actions.
For example, I can easily get the city like that:

>>> City.get("ca:qc:montreal")
{"id": "ca:qc:montreal", "yet_another_field": "bar"}

But it does not work for .all_pks().

Expected behaviour:

>>> list(Country.all_pks())
["ca"]
>>> list(Province.all_pks())
["ca:qc"]
>>> list(City.all_pks())
["ca:qc:montreal"]

Actual behaviour:

>>> list(Country.all_pks())
["ca"]
>>> list(Province.all_pks())
["qc"]
>>> list(City.all_pks())
["montreal"]

This behaviour is caused by this patch of code:

return (
key.split(":")[-1]
if isinstance(key, str)
else key.decode(cls.Meta.encoding).split(":")[-1]
async for key in cls.db().scan_iter(f"{key_prefix}*", _type="ReJSON-RL")
)

Upon finding the key, it splits the key by : and takes the last part of it. So places:city:ca:qc:montreal becomes montreal.

While it should just remove the key_prefix part. So that places:city:ca:qc:montreal becomes ca:qc:montreal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions