Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back-relation expand #73

Closed
MuslemRahimi opened this issue Nov 9, 2023 · 4 comments · Fixed by #66
Closed

Back-relation expand #73

MuslemRahimi opened this issue Nov 9, 2023 · 4 comments · Fixed by #66
Labels
documentation Improvements or additions to documentation duplicate This issue or pull request already exists

Comments

@MuslemRahimi
Copy link

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
Copy link
Collaborator

m29h commented Nov 9, 2023

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
Copy link
Author

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
Copy link
Collaborator

m29h commented Nov 10, 2023

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
Copy link
Collaborator

m29h commented Dec 13, 2023

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

@m29h m29h added documentation Improvements or additions to documentation duplicate This issue or pull request already exists labels Dec 13, 2023
@m29h m29h linked a pull request Dec 13, 2023 that will close this issue
m29h added a commit to m29h/pocketbase that referenced this issue Dec 13, 2023
vaphes pushed a commit that referenced this issue Feb 8, 2024
…75)

* Rename LogService methods and api endpoint url. Fixes #74

* accept empty result during integration test.
Rename tests to match new method names

* Add backrelation expansion test documenting the backrelation expansion functionality. Closes #73
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation duplicate This issue or pull request already exists
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants