vaphes / pocketbase

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

Expanding relation with Pocketbase version 1.13.1 leads to error? #35

Closed Vrajs16 closed 1 year ago

Vrajs16 commented 1 year ago

I am trying to expand a relation, however I am getting the following error.

  File "test.py", line 65, in test_func
    records = pb.collection("product_details").get_full_list(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pocketbase/services/utils/crud_service.py", line 17, in get_full_list
    return self._get_full_list(self.base_crud_path(), batch, query_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pocketbase/services/utils/base_crud_service.py", line 31, in _get_full_list
    return request(result, 1)
           ^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pocketbase/services/utils/base_crud_service.py", line 23, in request
    list = self._get_list(base_path, page, batch_size, query_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pocketbase/services/utils/base_crud_service.py", line 44, in _get_list
    items.append(self.decode(item))
                 ^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pocketbase/services/record_service.py", line 50, in decode
    return Record(data)
           ^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pocketbase/models/utils/base_model.py", line 16, in __init__
    self.load(data)
  File "/opt/homebrew/lib/python3.11/site-packages/pocketbase/models/record.py", line 18, in load
    self.load_expanded()
  File "/opt/homebrew/lib/python3.11/site-packages/pocketbase/models/record.py", line 26, in load_expanded
    self.expand[key] = self.parse_expanded(value)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pocketbase/models/record.py", line 22, in parse_expanded
    return cls(data)
           ^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pocketbase/models/utils/base_model.py", line 16, in __init__
    self.load(data)
  File "/opt/homebrew/lib/python3.11/site-packages/pocketbase/models/record.py", line 13, in load
    super().load(data)
  File "/opt/homebrew/lib/python3.11/site-packages/pocketbase/models/utils/base_model.py", line 30, in load
    self.id = data.pop("id", "")
              ^^^^^^^^^^^^^^^^^^
TypeError: pop expected at most 1 argument, got 2

After further testing with the source_code, I noticed that even though data is typed as dict, I am getting a list.

Here is the output I am printing inside the load function:

[{'collectionId': '32zt00urx7zeou4', 'collectionName': 'testcollection', 'created': '2023-03-19 18:26:45.604Z', 'id': 'rm74nish4xa4inm', 'image_url': 'https://www.example.com', 'updated': '2023-03-19 18:26:45.604Z'}, {'collectionId': '32zt00urx7zeou4', 'collectionName': 'testcollection', 'created': '2023-03-19 19:43:03.957Z', 'id': 'ju4srx55a5v7cmj', 'image_url': 'https://www.example.com', 'updated': '2023-03-19 19:43:03.957Z'}]

Here is my query to the pocketbase function get_full_list()

query_params={"expand": "image"}
m29h commented 1 year ago

Sorry but i cannot reproduce your issue with the current master branch of this repo. Are you sure that your record field "image" is present in the database as a relational field?

The test case

    def test_get_record_expand_full_list(self, client: PocketBase, state):
        rel = client.collection(state.coll.id).get_full_list(
            query_params={"expand": "rel"}
        )
        i = 0
        for r in rel:
            while r != None:
                if hasattr(r, "expand"):
                    if "rel" in r.expand:
                        i += 1
                        r = r.expand["rel"]
                        continue
                r = None
        assert i == 10

works well on a example database that has 10 records each with a relational field "rel". I just tested this to work both with a pocketbase v0.13.1 as well as the current pocketbase v0.16.4.

note even a recursive expand with depth six query_params={"expand": "rel.rel.rel.rel.rel.rel"} works well on my side

mrpn commented 1 year ago

I'm experiencing a similar problem, but it only happens when I try to expand a multiple-choice relationship. It just works fine for single relationship fields.