vaphes / pocketbase

PocketBase client SDK for python
https://pypi.org/project/pocketbase/
MIT License
359 stars 41 forks source link

TypeError when expanding relations. #65

Closed kbruegge closed 1 year ago

kbruegge commented 1 year ago

Hi there,

I've been trying to use client.collection("foo").get_list and client.collection("foo").get_full_list together with the expand query parameter.

See the documentation of the JS SDK here: https://pocketbase.io/docs/working-with-relations/#expanding-relations

Apparently the load method of the "BaseModel" class expects a dict but receives a list. I added some print statements.

def load(self, data: dict) -> None:
    """Loads `data` into the current model."""
    print(data, type(data))
    self.id = data.pop("id", "")
    self.created = to_datetime(data.pop("created", ""))
    self.updated = to_datetime(data.pop("updated", ""))

Here is the output:

In [2]: client.collection("labels").get_list(1, 30, query_params={"expand": "location"})

{'collectionId': 'c2laxzj0zheqs0l', 'collectionName': 'labels', 'created': '2023-08-07 10:06:32.374Z', 'end': '2023-04-06 23:30:00.000Z', 'expand': {'location': [{'collectionId': 'd4jq751z03smpzz', 'collectionName': 'pump_parts', 'created': '2023-08-07 08:52:03.363Z', 'id': '0yoeep2hyh0p57t', 'name': 'WB2', 'updated': '2023-08-07 08:52:03.363Z'}]}, 'id': '1hiin4yc2mzi5dd', 'label': ['l38hfht79whweza'], 'location': ['0yoeep2hyh0p57t'], 'monitoring_id': 18198, 'start': '2023-04-06 18:05:00.000Z', 'updated': '2023-08-07 10:06:32.374Z'} <class 'dict'>
[{'collectionId': 'd4jq751z03smpzz', 'collectionName': 'pump_parts', 'created': '2023-08-07 08:52:03.363Z', 'id': '0yoeep2hyh0p57t', 'name': 'WB2', 'updated': '2023-08-07 08:52:03.363Z'}] <class 'list'>

Which further results in the following exception

File <path>/pocketbase/models/utils/base_model.py:27, in BaseModel.load(self, data)
     25 """Loads `data` into the current model."""
     26 print(data, type(data))
---> 27 self.id = data.pop("id", "")
     28 self.created = to_datetime(data.pop("created", ""))
     29 self.updated = to_datetime(data.pop("updated", ""))

TypeError: pop expected at most 1 argument, got 2

Thanks for working on this by the way!

czwilling commented 1 year ago

I can confirm this. A fix would be great.

kbruegge commented 1 year ago

One thing I forgot to mention. The relation can point to multiple rows in the other collection. See the attached screenshot from the PocketBase admin interface.

Screenshot 2023-08-09 at 13 21 35
m29h commented 1 year ago

Relation expansion has a passing integration test here

I can see that this currently only works for a single record relation.

Can you confirm that your issue is caused only when there is a 'multi' relationship to a 'list of multiple record' relationships?

m29h commented 1 year ago

The PR #66 should fix this.

Can you please check if the following behaviour is what you want to see:

With PR #66 the value of Record.expand["xxx"] can either be any of

This is somewhat "highly dynamically typed" in a mapped 1:1 as it simply comes out of pocketbase.

kbruegge commented 1 year ago

Hi. Thanks for the PR. I will test it once I get back to work next tuesday.

kbruegge commented 1 year ago

PR #66 does indeed fix it. Thank you.