vaphes / pocketbase

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

Back-relation expand #73

Closed MuslemRahimi closed 9 months ago

MuslemRahimi commented 10 months ago

Does the SDK support the back-relation expand command of pocketbase.

In the JS SDK that would be

  output = await locals.pb.collection('posts').getList(data.startPage, 10, {
                sort: sort,
                filter: `tagline="${data?.filterTicker}" && pinned=false`,
                expand: `user,comments(post),alreadyVoted(post)`,
            }) ;

What would be the equivalent for "alreadyVoted(post)"?

m29h commented 10 months ago

You can pass your expand rule via the query_params named argument just like shown here

Example

The string is just passed on directly to Pocketbase without further processing. Not sure what pocketbase practically does when expanding a back relation. Naively I would expect this just increases the number of query results lines but maybe it just works like a regular expansion. Keep in mind that whatever is expanded can be found in the .expand['field_name'] property of the query result (like shown in the Example). Anyhow maybe just give it a try like in the Example and see what happens. If you get an unexpected result we can have a second look!

MuslemRahimi commented 10 months ago

Not working! My code looks like this:

output = pb.collection('posts').get_list(start_page,5, query_params={
            'sort': sort,
            'filter': filtering,
            'expand': 'user, comments(post)',
            })

I get the error:

  File "/home/mrahimi/.local/lib/python3.10/site-packages/pocketbase/models/utils/base_model.py", line 26, in load
    self.id = data.pop("id", "")
TypeError: pop expected at most 1 argument, got 2
m29h commented 10 months ago

This now looks very related to issue #65 where a Multirelation forward expansion caused the same fault as you experience. Are you sure you use a version that has the fix to issue #65 integrated?

Of course possible the backward relationship expansion works somehow different in some subtle way....

m29h commented 9 months ago

Hi, i looked into this and backrelation expansion seems to work perfectly fine (just indentical to forward relation expansion)

I drafted a new unit test for this to be added in /tests/integration/test_record.py at some point to both verify and document the functionality.

    def test_get_record_backrelation_expand(self, client: PocketBase, state):
        backrel_field = "%s(rel)" % state.coll.id
        rel = client.collection(state.coll.id).get_one(
            state.chained_records[0],
            {"expand": ".".join([backrel_field] * 6)},
        )
        for i, r in enumerate(state.chained_records):
            if isinstance(rel, list):
                assert len(rel) == 1
                rel = rel[0]
            assert rel.id == r
            if i > 5:
                break
            rel = rel.expand[backrel_field]

the unit tests shows that it is even possible to expand the backrelation 6 levels deep. This backrelation expansion is a pretty amazing feature by the way that i did not know of :-)

As i mentioned previously the error you get is probably from using a (earlier) version of this library that does not yet have the fix to #65