supabase / postgrest-py

PostgREST client for Python. This library provides an ORM interface to PostgREST
https://postgrest-py.readthedocs.io
MIT License
236 stars 52 forks source link

fix: fixed the 'order' method for 'BaseSelectRequestBuilder' #495

Closed Nikdedov closed 3 months ago

Nikdedov commented 3 months ago

Fixed the 'order' method for 'BaseSelectRequestBuilder' to support multiple sorting criteria and handle sorting on foreign tables.

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

Issue: https://github.com/supabase/supabase-py/issues/775

response = (
    supabase.table("clones")
    .select("*")
    .eq("owner", uid)
    .order("last_chatted", desc=True)
    .order("created_at", desc=True)
    .execute()
)

The problem is that adding several orders generates for QueryParams this: &order=last_chatted.desc&order=created_at.desc

Previous workaround:

request_build=
    supabase.table("clones")
    .select("*")
    .eq("owner", uid)
    .order("last_chatted", desc=True)
    .order("created_at", desc=True)

#get list of all added orders
removed_orders=request_build.params.get_list('order')

#remove them from QueryParams
request_build.params=request_build.params.remove('order')

#add back by merging them
request_build.params=request_build.params.add('order',','.join(removed_orders))
response =request_build.execute()

What is the new behavior?

response = (
    supabase.table("clones")
    .select("*")
    .eq("owner", uid)
    .order("last_chatted", desc=True)
    .order("created_at", desc=True)
    .execute()
)

Works as expected and generates QueryParams according to postgrest docs and it looks like: &order=last_chatted.desc,created_at.desc

Additional context

Add any other context or screenshots.